Challenge - 5 Problems
mutate() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mutate() adding a new column
What is the output of this R code using
dplyr::mutate() to add a new column?R Programming
library(dplyr) df <- tibble(x = 1:3, y = 4:6) df_new <- df %>% mutate(z = x + y) df_new
Attempts:
2 left
💡 Hint
mutate() adds new columns by evaluating expressions using existing columns.
✗ Incorrect
mutate() creates a new column 'z' by adding columns 'x' and 'y' element-wise.
❓ Predict Output
intermediate2:00remaining
Using mutate() with conditional new column
What is the output of this R code that adds a new column with a condition inside mutate()?
R Programming
library(dplyr) df <- tibble(score = c(45, 75, 85)) df_new <- df %>% mutate(pass = ifelse(score >= 50, "yes", "no")) df_new
Attempts:
2 left
💡 Hint
ifelse(condition, true_value, false_value) returns values based on condition for each row.
✗ Incorrect
The pass column is "yes" if score >= 50, otherwise "no".
🔧 Debug
advanced2:00remaining
Identify the error in mutate() code
What error does this R code produce when trying to add a new column with mutate()?
R Programming
library(dplyr) df <- tibble(a = 1:3) df %>% mutate(b = a + c)
Attempts:
2 left
💡 Hint
Check if all variables used inside mutate() exist in the data frame or environment.
✗ Incorrect
The variable 'c' is not defined anywhere, so mutate() cannot compute 'b = a + c'.
❓ Predict Output
advanced2:00remaining
Result of mutate() with multiple new columns
What is the output of this R code that adds two new columns using mutate()?
R Programming
library(dplyr) df <- tibble(x = 2:4) df_new <- df %>% mutate(y = x^2, z = y + 1) df_new
Attempts:
2 left
💡 Hint
mutate() evaluates new columns in order, so 'z' can use 'y' defined earlier.
✗ Incorrect
Column y is x squared, then z is y plus 1, so z values are 5, 10, 17.
🧠 Conceptual
expert2:00remaining
Understanding mutate() column evaluation order
Which statement correctly describes how mutate() evaluates new columns when multiple are added?
Attempts:
2 left
💡 Hint
Think about how you can use a new column just created in the same mutate() call.
✗ Incorrect
mutate() evaluates new columns in order, so each can use columns created before it.