Bird
Raised Fist0
R Programmingprogramming~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the mutate() function do in R's dplyr package?
easy
A. It sorts the data frame by a column.
B. It deletes rows from a data frame.
C. It merges two data frames together.
D. It adds new columns or modifies existing ones in a data frame.

Solution

  1. Step 1: Understand the purpose of mutate()

    The mutate() function is designed to add new columns or change existing columns in a data frame.
  2. Step 2: Compare with other functions

    Deleting rows is done by filter(), sorting by arrange(), and merging by join functions, not mutate().
  3. Final Answer:

    It adds new columns or modifies existing ones in a data frame. -> Option D
  4. Quick Check:

    mutate() = add/change columns [OK]
Hint: mutate() changes or adds columns, not rows or merges [OK]
Common Mistakes:
  • Confusing mutate() with filter() for rows
  • Thinking mutate() merges data frames
  • Assuming mutate() sorts data
2. Which of the following is the correct syntax to add a new column total as the sum of columns a and b using mutate()?
easy
A. mutate(df, total = a + b)
B. mutate(df, total <- a + b)
C. mutate(df, total = sum(a, b))
D. mutate(df, total = a & b)

Solution

  1. Step 1: Check assignment inside mutate()

    Inside mutate(), new columns are created with =, not with assignment operator <-.
  2. Step 2: Understand how to sum columns element-wise

    To add columns element-wise, use a + b. Using sum(a, b) sums all values together, not element-wise. a & b is a logical AND, not addition.
  3. Final Answer:

    mutate(df, total = a + b) -> Option A
  4. Quick Check:

    Use = and + for element-wise sum [OK]
Hint: Use = and + inside mutate() for new column sums [OK]
Common Mistakes:
  • Using <- instead of = inside mutate()
  • Using sum() instead of + for element-wise addition
  • Using & instead of + for addition
3. Given the data frame df <- data.frame(x = 1:3, y = 4:6), what will be the result of mutate(df, z = x * y)?
medium
A. A data frame with columns x, y, and z where z is the product of x and y.
B. A data frame with columns x and y only, no new column added.
C. An error because mutate() cannot multiply columns.
D. A data frame with column z only containing the product values.

Solution

  1. Step 1: Understand mutate() behavior

    mutate() adds new columns without removing existing ones unless explicitly overwritten.
  2. Step 2: Calculate new column values

    For each row, z = x * y means multiplying corresponding elements: 1*4=4, 2*5=10, 3*6=18.
  3. Final Answer:

    A data frame with columns x, y, and z where z is the product of x and y. -> Option A
  4. Quick Check:

    mutate() adds z = x * y element-wise [OK]
Hint: mutate() keeps old columns and adds new ones [OK]
Common Mistakes:
  • Expecting mutate() to remove old columns
  • Thinking mutate() cannot do arithmetic
  • Assuming new column replaces all columns
4. What is wrong with this code snippet?
df <- data.frame(a = 1:3, b = 4:6)
df %>% mutate(c = a + d)
medium
A. The syntax of mutate() is incorrect; it needs a comma after c.
B. Column d does not exist in df, causing an error.
C. You cannot use the pipe operator %>% with mutate().
D. The code will run but c will be filled with NA values.

Solution

  1. Step 1: Check column names in data frame

    The data frame df has columns a and b, but no column named d.
  2. Step 2: Understand mutate() behavior with missing columns

    Trying to use a non-existent column d inside mutate() causes an error because R cannot find it.
  3. Final Answer:

    Column d does not exist in df, causing an error. -> Option B
  4. Quick Check:

    Using unknown columns in mutate() causes error [OK]
Hint: Check all columns exist before using in mutate() [OK]
Common Mistakes:
  • Assuming mutate() creates missing columns automatically
  • Thinking pipe operator %>% is invalid here
  • Believing syntax error due to missing comma
5. You have a data frame df with columns score1 and score2. You want to add a new column average that is the average of these two scores, but only if both scores are greater than 0; otherwise, average should be NA. Which mutate() code correctly does this?
hard
A. mutate(df, average = (score1 + score2) / 2)
B. mutate(df, average = ifelse(score1 > 0 | score2 > 0, (score1 + score2) / 2, NA))
C. mutate(df, average = ifelse(score1 > 0 & score2 > 0, (score1 + score2) / 2, NA))
D. mutate(df, average = ifelse(score1 & score2, (score1 + score2) / 2, NA))

Solution

  1. Step 1: Use ifelse() with correct condition

    The condition requires both score1 and score2 to be greater than 0, so use vectorized & inside ifelse().
  2. Step 2: Calculate average or assign NA

    If condition is true, calculate average as (score1 + score2) / 2, else assign NA.
  3. Final Answer:

    mutate(df, average = ifelse(score1 > 0 & score2 > 0, (score1 + score2) / 2, NA)) -> Option C
  4. Quick Check:

    Use ifelse() with & for both conditions [OK]
Hint: Use ifelse() with & to check both conditions [OK]
Common Mistakes:
  • Using | instead of & to require both conditions
  • Omitting ifelse() and always calculating average
  • Using score1 & score2 without comparison
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard