Challenge - 5 Problems
R Vector and List Modifier
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this R code modifying a vector element?
Consider the following R code that modifies an element in a vector. What will be printed?
R Programming
vec <- c(10, 20, 30, 40) vec[2] <- 25 print(vec)
Attempts:
2 left
💡 Hint
Remember that indexing in R starts at 1 and you can assign a new value to a specific position.
✗ Incorrect
The code changes the second element of the vector from 20 to 25. So the vector becomes 10, 25, 30, 40.
❓ Predict Output
intermediate2:00remaining
What happens when adding a new element to a list in R?
What will be the output of this R code that adds a new element to a list?
R Programming
my_list <- list(a=1, b=2) my_list$c <- 3 print(my_list)
Attempts:
2 left
💡 Hint
In R, you can add named elements to a list using the $ operator.
✗ Incorrect
The code adds a new element named 'c' with value 3 to the list. Printing the list shows all elements with their names.
🔧 Debug
advanced2:00remaining
Why does this R code produce an error when modifying a vector element?
Identify the error in this R code and explain why it happens.
R Programming
vec <- c(1, 2, 3) vec[4] <- 5 print(vec)
Attempts:
2 left
💡 Hint
In R, vectors can be extended by assigning to an index beyond their current length.
✗ Incorrect
Assigning to vec[4] adds a new element 5 at position 4. The vector becomes 1, 2, 3, 5 without error.
❓ Predict Output
advanced2:00remaining
What is the output after modifying a data frame column in R?
Given this R code that modifies a data frame column, what will be printed?
R Programming
df <- data.frame(x = 1:3, y = c('a', 'b', 'c')) df$x[2] <- 10 print(df)
Attempts:
2 left
💡 Hint
You can modify specific elements of a data frame column by indexing.
✗ Incorrect
The second element of column x is changed from 2 to 10. Other values remain unchanged.
🧠 Conceptual
expert2:00remaining
How does R handle adding elements beyond current vector length?
In R, what happens when you assign a value to an index beyond the current length of a vector?
Attempts:
2 left
💡 Hint
Think about what happens if you assign to an index that does not yet exist in the vector.
✗ Incorrect
R automatically extends the vector to the new length, filling any new positions with NA before assigning the new value.