Loops and Apply
Apply Family
Summarizing a List
Apply-style functions run one operation across pieces of data. sapply simplifies the result when it can.
Program
Play the script to sum each list element and select one named total.
apply_family.R
values <- list(a = 1:3, b = 4:5)
totals <- sapply(values, sum)
answer <- totals["b"]
cat(answer, "\n", sep = "")
list
A list can hold vectors of different lengths under names.
sapply
`sapply(values, sum)` runs `sum` for each list element.
named vector
The result keeps names such as `a` and `b`.