Explain the use of the apply() family of functions in R.
Instruction: Describe a scenario where you would use lapply() over sapply(), and explain the difference between these functions.
Context: Tests predictable result types when applying a function over a list, including when simplification is useful or unsafe.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
lapply applies a function to each list element and returns a list of the same length. sapply starts similarly, then tries to simplify compatible results into a vector or matrix. Its return shape can therefore change when the input changes.
items <- list(a = c(1, NA, 3), b = c(4, 5)) clean <- function(x) x[!is.na(x)]...