0
0
R Programmingprogramming~5 mins

mutate() for new columns in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the mutate() function do in R's dplyr package?

mutate() adds new columns or changes existing ones in a data frame without removing existing data.

Click to reveal answer
beginner
How do you create a new column total as the sum of columns price and tax using mutate()?
df %>% mutate(total = price + tax)
Click to reveal answer
beginner
Can mutate() use existing columns to create new ones?

Yes, mutate() can use existing columns to calculate values for new columns.

Click to reveal answer
intermediate
What happens if you use mutate() to create a column with the same name as an existing one?

The existing column is replaced by the new values you provide.

Click to reveal answer
beginner
Write a mutate() example that creates a new column discounted_price which is 10% less than price.
df %>% mutate(discounted_price = price * 0.9)
Click to reveal answer
What is the main purpose of mutate() in dplyr?
AFilter rows based on conditions
BSummarize data by groups
CSort rows by column values
DAdd or modify columns in a data frame
Which of the following creates a new column area as length * width using mutate()?
Adf %>% mutate(area = length + width)
Bdf %>% filter(area = length * width)
Cdf %>% mutate(area = length * width)
Ddf %>% select(area = length * width)
If you want to replace an existing column score with its square, which is correct?
Adf %>% mutate(new_score = score^2)
Bdf %>% mutate(score = score^2)
Cdf %>% filter(score = score^2)
Ddf %>% select(score = score^2)
Can mutate() create multiple new columns at once?
AYes, by separating each new column with a comma
BNo, only one column at a time
COnly if you use <code>summarize()</code> instead
DOnly if you use <code>filter()</code> first
What will this code do? df %>% mutate(new_col = NA)
ACreate a new column <code>new_col</code> filled with missing values
BDelete the column <code>new_col</code>
CFilter rows where <code>new_col</code> is NA
DRename <code>new_col</code> to NA
Explain how to use mutate() to add a new column based on existing columns in a data frame.
Think about how you create a new column that depends on others.
You got /4 concepts.
    Describe what happens if you use mutate() to create a column with the same name as an existing one.
    Consider how mutate handles column names.
    You got /4 concepts.