Using mutate to change or create a column
Suppose we want life expectancy to be measured in months instead of years: you'd have to multiply the existing value by 12. You can use the mutate()
verb to change this column, or to create a new column that's calculated this way.
This is a part of the course
“Introduction to the Tidyverse”
Exercise instructions
- Use
mutate()
to change the existinglifeExp
column, by multiplying it by 12:12 * lifeExp
. - Use
mutate()
to add a new column, calledlifeExpMonths
, calculated as12 * lifeExp
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
library(gapminder)
library(dplyr)
# Use mutate to change lifeExp to be in months
# Use mutate to create a new column called lifeExpMonths