0
0
R Programmingprogramming~20 mins

mutate() for new columns in R Programming - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
mutate() Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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
AError: object 'z' not found
BA tibble with columns x, y, z where z is the product of x and y: rows (1,4,4), (2,5,10), (3,6,18)
CA tibble with columns x, y, z where z is the sum of x and y: rows (1,4,5), (2,5,7), (3,6,9)
DA tibble with columns x, y only, no z column added
Attempts:
2 left
💡 Hint
mutate() adds new columns by evaluating expressions using existing columns.
Predict Output
intermediate
2: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
AA tibble with columns score and pass: (45, "yes"), (75, "no"), (85, "no")
BA tibble with columns score and pass: (45, "no"), (75, "yes"), (85, "yes")
CA tibble with only the score column, no pass column added
DError: unexpected symbol in ifelse statement
Attempts:
2 left
💡 Hint
ifelse(condition, true_value, false_value) returns values based on condition for each row.
🔧 Debug
advanced
2: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)
ANo error, adds column b with values 1, 2, 3
BError: unexpected symbol in mutate()
CError: column 'a' not found
DError: object 'c' not found
Attempts:
2 left
💡 Hint
Check if all variables used inside mutate() exist in the data frame or environment.
Predict Output
advanced
2: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
AA tibble with columns x, y, z: rows (2,4,5), (3,9,10), (4,16,17)
BA tibble with columns x, y, z: rows (2,4,4), (3,9,9), (4,16,16)
CError: object 'y' not found inside mutate()
DA tibble with columns x and y only, no z column
Attempts:
2 left
💡 Hint
mutate() evaluates new columns in order, so 'z' can use 'y' defined earlier.
🧠 Conceptual
expert
2:00remaining
Understanding mutate() column evaluation order
Which statement correctly describes how mutate() evaluates new columns when multiple are added?
ANew columns are evaluated sequentially from left to right, so later columns can use earlier ones.
BNew columns are evaluated in reverse order, so earlier columns can use later ones.
Cmutate() only allows adding one new column at a time; multiple columns cause an error.
DAll new columns are evaluated simultaneously, so none can use others defined in the same mutate().
Attempts:
2 left
💡 Hint
Think about how you can use a new column just created in the same mutate() call.