0
0
R Programmingprogramming~20 mins

Apply family vs loops in R Programming - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Apply Family Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of lapply vs for loop
What is the output of the following R code snippet?
R Programming
x <- list(a = 1:3, b = 4:6)
result_lapply <- lapply(x, sum)
result_loop <- list()
for (name in names(x)) {
  result_loop[[name]] <- sum(x[[name]])
}
result_lapply
result_loop
Aresult_lapply and result_loop are identical lists with elements 6 and 15
Bresult_lapply is a numeric vector, result_loop is a list with sums
Cresult_lapply is a list with sums, result_loop is a numeric vector
DBoth result_lapply and result_loop are numeric vectors
Attempts:
2 left
💡 Hint
Remember that lapply returns a list and for loop can build a list similarly.
Predict Output
intermediate
2:00remaining
Output difference between sapply and for loop
What is the output of this R code?
R Programming
x <- list(a = 1:3, b = 4:6)
result_sapply <- sapply(x, sum)
result_loop <- numeric(length(x))
names(result_loop) <- names(x)
for (i in seq_along(x)) {
  result_loop[i] <- sum(x[[i]])
}
result_sapply
result_loop
Aresult_sapply is a numeric vector, result_loop is a list
BBoth result_sapply and result_loop are lists
Cresult_sapply is a list, result_loop is a numeric vector
DBoth result_sapply and result_loop are named numeric vectors with values 6 and 15
Attempts:
2 left
💡 Hint
sapply tries to simplify the result to a vector if possible.
🧠 Conceptual
advanced
1:30remaining
Why use apply family over loops?
Which of the following is the best reason to prefer apply functions over for loops in R?
AFor loops cannot handle lists or data frames
BApply functions are always faster than for loops in every case
CApply functions provide concise code and often better readability
DApply functions automatically parallelize code without extra setup
Attempts:
2 left
💡 Hint
Think about code clarity and style rather than speed alone.
Predict Output
advanced
1:30remaining
Output of mapply with multiple arguments
What is the output of this R code?
R Programming
x <- 1:3
y <- 4:6
result <- mapply(function(a, b) a * b, x, y)
result
AAn error because mapply requires a list input
BA numeric vector: 4, 10, 18
CA numeric vector: 5, 7, 9
DA list: list(4, 10, 18)
Attempts:
2 left
💡 Hint
mapply applies the function element-wise over multiple vectors.
🔧 Debug
expert
2:00remaining
Identify the error in this apply usage
What error does this R code produce and why?
R Programming
mat <- matrix(1:9, nrow=3)
result <- apply(mat, 2, function(x) x + y)
result
AError: object 'y' not found because y is not defined
BError: incorrect margin argument in apply
CNo error, returns matrix with y added to each column
DError: function must return a scalar value
Attempts:
2 left
💡 Hint
Check if all variables used inside the function are defined.