Explain the concept of vectorization in R.
Instruction: Define vectorization and provide an example of how it can optimize data operations in R.
Context: This question assesses the candidate's understanding of one of R's core principles for efficient computation.
Updated
Official answer available
Read the opening below, then unlock the full answer and practical guidance.
Vectorized operations accept whole vectors and perform the corresponding computation without an explicit R-level loop at the call site. Native implementations often reduce interpreter overhead, but they can still allocate large temporary vectors.
numbers <- c(1, 2, 3, NA_real_) loop_result <- numeric(length(numbers)) for (i in seq_along(numbers)) loop_result[i] <-...