Code from class

code
Week03
Published

February 3, 2026

apply_to <- function(d, f, ...) {
  sapply(d, f)
}

apply_to(mtcars, class)

library(ggplot2)
apply_to(txhousing, class)

num_missing <- function(x) {
  sum(is.na(x))
}

apply_to(txhousing, num_missing)

apply_to(txhousing, function(x) sum(is.na(x)))

apply_to(txhousing, f = function(x) { sum(is.na(x)) })

apply_to(txhousing, f = mean)
apply_to(txhousing, f = mean, na.rm=TRUE)

sapply(txhousing, FUN = mean)

sapply(txhousing, FUN = mean, na.rm=TRUE)

sapply(na.rm=TRUE, FU = mean,  X = diamonds)
sapply(FU = class,  X = diamonds)




apply_to(diamonds, f = mean)
apply_to(mtcars, f = mean)



apply_to <- function(d, f) {
#  browser()
  res <- c()
  for (i in 1:length(d)) {
    res[i] <- f(d[[i]])
    names(res)[i] <- names(d[i])[1]
  }
  res
}





apply_to <- function(d, f, ...) {
  res <- c()
  dots <- length(dots_list())
  for (i in 1:length(d)) {
    if (dots > 0) res[i] <- f(d[[i]], ...)
    else res[i] <- f(d[[i]])
    names(res)[i] <- names(d[i])[1]
  }
  res
}