Functional Programming in R
Instruction: Discuss the principles of functional programming in R and how they can be applied to data analysis.
Context: This question tests the candidate's understanding of functional programming concepts in R, emphasizing code simplicity and efficiency.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
R supports first-class functions and higher-order operations such as lapply, Map and Reduce. Functional style emphasizes explicit inputs/outputs, composition and minimizing hidden side effects; R does not enforce universal immutability or purity.
summarize_vector <- function(x) { stopifnot(is.numeric(x)) valid <- x[!is.na(x)] list(observed = length(valid), mean = if (length(valid)) mean(valid) else NA_real_) } input <- list(a = c(1, NA, 3),...