title: “Week 12: Code Along 11” subtitle: “R For Data Science: Chapter 19” author: “Declan Fogarty” date: “2025-06-17” output: html_document editor_options: chunk_output_type: console —

Functions

Introduction

When should you write a function?

rescale01 <- function(x) {
  rng <- range(x, na.rm = TRUE)
  (x - rng[1]) / (rng[2] - rng[1])
}
set.seed(1234)
df <- tibble::tibble(
  a = rnorm(10),
  b = rnorm(10),
  c = rnorm(10),
  d = rnorm(10)
)
df$a <- (df$a - min(df$a, na.rm = TRUE)) / 
  (max(df$a, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$b <- (df$b - min(df$b, na.rm = TRUE)) / 
  (max(df$b, na.rm = TRUE) - min(df$a, na.rm = TRUE))
df$c <- (df$c - min(df$c, na.rm = TRUE)) / 
  (max(df$c, na.rm = TRUE) - min(df$c, na.rm = TRUE))
df$d <- (df$d - min(df$d, na.rm = TRUE)) / 
  (max(df$d, na.rm = TRUE) - min(df$d, na.rm = TRUE))
rescale01 <- function(x) {
  rng <- range(x, na.rm = TRUE)
  (x - rng[1]) / (rng[2] - rng[1])
}
rescale01(c(0, 5, 10))
## [1] 0.0 0.5 1.0
#> [1] 0.0 0.5 1.0

rescale01(c(-10, 0, 10))
## [1] 0.0 0.5 1.0
#> [1] 0.0 0.5 1.0
rescale01(c(1, 2, 3, NA, 5))
## [1] 0.00 0.25 0.50   NA 1.00
#> [1] 0.00 0.25 0.50   NA 1.00
df$a <- rescale01(df$a)
df$b <- rescale01(df$b)
df$c <- rescale01(df$c)
df$d <- rescale01(df$d)

Functions are for humans and computers

Conditional execution

has_name <- function(x) {
  nms <- names(x)
  if (is.null(nms)) {
    rep(FALSE, length(x))
  } else {
    !is.na(nms) & nms != ""
  }
}

Function arguments

# Compute confidence interval around mean using normal approximation
mean_ci <- function(x, conf = 0.95) {
  se <- sd(x) / sqrt(length(x))
  alpha <- 1 - conf
  mean(x) + se * qnorm(c(alpha / 2, 1 - alpha / 2))
}

x <- runif(100)
mean_ci(x)
## [1] 0.4297563 0.5442853
#> [1] 0.4976111 0.6099594
mean_ci(x, conf = 0.99)
## [1] 0.4117625 0.5622791
#> [1] 0.4799599 0.6276105

# Good
mean(1:10, na.rm = TRUE)
## [1] 5.5
# Bad
mean(x = 1:10, , FALSE)
## [1] 5.5
mean(, TRUE, x = c(1:10, NA))
## [1] 5.5

Return values