# ============================================================
# The apply Family
# R Foundations — Rverse Analytics
# https://rverseanalytics.com/learn/apply
# ============================================================

# The apply family - loops without loops
# 1. sapply: one result per element
sapply(1:5, sqrt)
# 2. Use your own function
sapply(c(4, 9, 25), function(x) x + 1)
# 3. lapply returns a list
lapply(c("r", "stats"), toupper)
# 4. apply: rows (1) or columns (2) of a matrix
m <- matrix(1:6, nrow = 2)
apply(m, 1, sum)
apply(m, 2, max)
# 5. tapply: summarise by group
dose <- c(10, 20, 15, 25)
grp <- c("a", "a", "b", "b")
tapply(dose, grp, mean)
