The apply Family
The apply family applies a function to every element of a list or vector, the same job map() does elsewhere — the differences between its members are entirely about the shape of what comes back. lapply() always returns a list, no matter what the function produces. sapply() tries to simplify that list into a plain vector (or matrix) when every result is the same length and type, which is usually what you want for quick use but is not fully predictable in the general case. vapply() is sapply() with the result shape declared up front (numeric(1) here means "exactly one number per element") — slightly more to write, but it fails loudly instead of silently if a result doesn't match, which makes it the safer choice inside real code.
nums <- list(1:3, 4:6, 7:9) print(sapply(nums, sum)) print(lapply(nums, sum)) print(vapply(nums, sum, numeric(1)))
[1] 6 15 24 [[1]] [1] 6 [[2]] [1] 15 [[3]] [1] 24 [1] 6 15 24
All three calls computed the exact same three sums (6, 15, 24) from the exact same input, a list of three ranges — but lapply() handed them back wrapped in a list with [[1]], [[2]], [[3]] labels, while sapply() and vapply() simplified the result down to a plain numeric vector. Next: NA handling — how missing values propagate through arithmetic, and is.na() / na.rm as the tools for dealing with them deliberately.
Practice exercise
Create words <- list("hi", "hello", "hey"), then use sapply() to print the length of each word with nchar.