Challenge - 5 Problems
Advanced R Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nested function calls with advanced features
What is the output of this R code using advanced features like closures and environments?
R Programming
make_counter <- function() {
count <- 0
function() {
count <<- count + 1
count
}
}
counter <- make_counter()
c(counter(), counter(), counter())Attempts:
2 left
💡 Hint
Think about how the inner function changes the variable 'count' in its environment.
✗ Incorrect
The inner function increments 'count' stored in the parent environment each time it is called, so the outputs are 1, 2, and 3.
❓ Predict Output
intermediate1:30remaining
Output of vectorized operations with recycling
What is the output of this R code that uses vector recycling in arithmetic?
R Programming
x <- c(1, 2, 3, 4) y <- c(10, 20) x + y
Attempts:
2 left
💡 Hint
Remember how R recycles shorter vectors to match the length of longer ones.
✗ Incorrect
The shorter vector y is recycled to match x: (10,20,10,20). Adding element-wise gives (11,22,13,24).
🔧 Debug
advanced1:30remaining
Identify the error in advanced list manipulation
What error does this R code produce when trying to access a nested list element?
R Programming
my_list <- list(a = list(b = 1:3)) my_list$a$b[4]
Attempts:
2 left
💡 Hint
Check what happens when you access an index beyond the vector length in R.
✗ Incorrect
Accessing an out-of-bounds index in a vector returns NA without an error.
🧠 Conceptual
advanced1:30remaining
Why use environments for complex R programming?
Which statement best explains why environments enable complex work in R?
Attempts:
2 left
💡 Hint
Think about how environments manage variable visibility and lifetime.
✗ Incorrect
Environments let you keep variables separate and control their scope, which helps build complex programs with state.
❓ Predict Output
expert2:30remaining
Output of advanced metaprogramming with substitute and eval
What is the output of this R code using substitute and eval for metaprogramming?
R Programming
x <- 10 expr <- substitute(x + y) y <- 5 eval(expr)
Attempts:
2 left
💡 Hint
Consider when substitute captures the expression and when eval evaluates it.
✗ Incorrect
substitute captures the expression 'x + y' unevaluated. Later, eval evaluates it with y=5, so output is 15.