dplyr mutate(): How to Add a Column in R
mutate() adds new columns to a data frame or changes the ones you already have. Every calculation in a data analysis, from percentages to labels, starts here.
What does mutate() do?
mutate() creates or changes columns. The number of rows stays the same, because each row gets its own new value. You write the new column name, an equals sign, and the calculation:
mutate(data, new_column = calculation)
With the pipe from the pipe operator lesson:
data |> mutate(new_column = calculation)
The new column is added at the right-hand end of the table unless you say otherwise. This lesson is part of our dplyr tutorial. To choose existing columns, see dplyr select, and to choose rows, see dplyr filter.
Sample data
We use the ten US cities again. To keep the output narrow, this table has five columns. Populations are rounded Census figures in thousands, for practice.
library(dplyr)
cities <- tibble(
city = c("New York", "Los Angeles", "Chicago", "Houston", "Phoenix",
"Philadelphia", "San Antonio", "San Diego", "Dallas", "Austin"),
state = c("NY", "CA", "IL", "TX", "AZ", "PA", "TX", "CA", "TX", "TX"),
region = c("Northeast", "West", "Midwest", "South", "West",
"Northeast", "South", "West", "South", "South"),
population_2010 = c(8175, 3793, 2696, 2099, 1446, 1526, 1327, 1307, 1198, 790),
population_2020 = c(8804, 3899, 2746, 2305, 1608, 1604, 1435, 1387, 1304, 962)
)
| city | state | region | population_2010 | population_2020 |
|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 |
| Los Angeles | CA | West | 3793 | 3899 |
| Chicago | IL | Midwest | 2696 | 2746 |
| Houston | TX | South | 2099 | 2305 |
| Phoenix | AZ | West | 1446 | 1608 |
| Philadelphia | PA | Northeast | 1526 | 1604 |
| San Antonio | TX | South | 1327 | 1435 |
| San Diego | CA | West | 1307 | 1387 |
| Dallas | TX | South | 1198 | 1304 |
| Austin | TX | South | 790 | 962 |
Add your first column
Start with the simplest possible new column. Populations are in thousands. Let us add the 2020 population in millions, which is the number divided by 1000:
cities |> mutate(population_millions = population_2020 / 1000)
The name before = is the new column. The calculation after it can use any existing column. dplyr does the maths for every row at once:
| city | state | region | population_2010 | population_2020 | population_millions |
|---|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 | 8.804 |
| Los Angeles | CA | West | 3793 | 3899 | 3.899 |
| Chicago | IL | Midwest | 2696 | 2746 | 2.746 |
| Houston | TX | South | 2099 | 2305 | 2.305 |
| Phoenix | AZ | West | 1446 | 1608 | 1.608 |
| Philadelphia | PA | Northeast | 1526 | 1604 | 1.604 |
| San Antonio | TX | South | 1327 | 1435 | 1.435 |
| San Diego | CA | West | 1307 | 1387 | 1.387 |
| Dallas | TX | South | 1198 | 1304 | 1.304 |
| Austin | TX | South | 790 | 962 | 0.962 |
That is how you add a column in R with dplyr. The original cities is not changed. If you want to keep the new column, assign the result back:
cities <- cities |> mutate(population_millions = population_2020 / 1000)
For the rest of this lesson, we leave cities as it was above so that every example starts from the same table.
Calculate with existing columns
You can use any arithmetic operator: +, -, *, / and ^. How many more people lived in each city in 2020 than in 2010?
cities |> mutate(growth = population_2020 - population_2010)
| city | state | region | population_2010 | population_2020 | growth |
|---|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 | 629 |
| Los Angeles | CA | West | 3793 | 3899 | 106 |
| Chicago | IL | Midwest | 2696 | 2746 | 50 |
| Houston | TX | South | 2099 | 2305 | 206 |
| Phoenix | AZ | West | 1446 | 1608 | 162 |
| Philadelphia | PA | Northeast | 1526 | 1604 | 78 |
| San Antonio | TX | South | 1327 | 1435 | 108 |
| San Diego | CA | West | 1307 | 1387 | 80 |
| Dallas | TX | South | 1198 | 1304 | 106 |
| Austin | TX | South | 790 | 962 | 172 |
The growth column is in thousands too. Austin gained 172 thousand people, and Chicago just 50 thousand.
Create multiple columns
To mutate multiple columns, add several named calculations to the same call, separated by commas. Later ones can use columns created earlier in the same mutate():
cities |> mutate(
growth = population_2020 - population_2010,
growth_pct = round(growth / population_2010 * 100, 1)
)
| city | state | region | population_2010 | population_2020 | growth | growth_pct |
|---|---|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 | 629 | 7.7 |
| Los Angeles | CA | West | 3793 | 3899 | 106 | 2.8 |
| Chicago | IL | Midwest | 2696 | 2746 | 50 | 1.9 |
| Houston | TX | South | 2099 | 2305 | 206 | 9.8 |
| Phoenix | AZ | West | 1446 | 1608 | 162 | 11.2 |
| Philadelphia | PA | Northeast | 1526 | 1604 | 78 | 5.1 |
| San Antonio | TX | South | 1327 | 1435 | 108 | 8.1 |
| San Diego | CA | West | 1307 | 1387 | 80 | 6.1 |
| Dallas | TX | South | 1198 | 1304 | 106 | 8.8 |
| Austin | TX | South | 790 | 962 | 172 | 21.8 |
The second line uses growth, which was only created on the line above. round(x, 1) rounds to one decimal place. Austin grew by 21.8%, the fastest of the ten. Put one column per line, as above, so that the code is easy to read.
To apply the same calculation to many columns, across() saves typing. It has its own lesson: dplyr across.
Create text columns
Columns can hold text. paste() joins pieces together, and sep sets what goes between them:
cities |> mutate(label = paste(city, state, sep = ", "))
| city | state | label |
|---|---|---|
| New York | NY | New York, NY |
| Los Angeles | CA | Los Angeles, CA |
| Chicago | IL | Chicago, IL |
| Houston | TX | Houston, TX |
| Phoenix | AZ | Phoenix, AZ |
| Philadelphia | PA | Philadelphia, PA |
| San Antonio | TX | San Antonio, TX |
| San Diego | CA | San Diego, CA |
| Dallas | TX | Dallas, TX |
| Austin | TX | Austin, TX |
The related paste0() joins with no separator at all. This is handy for building labels such as paste0(city, " (", state, ")").
A single value, such as a country name, is repeated for every row:
cities |> mutate(country = "USA")
| city | state | country |
|---|---|---|
| New York | NY | USA |
| Los Angeles | CA | USA |
| Chicago | IL | USA |
| Houston | TX | USA |
| Phoenix | AZ | USA |
| Philadelphia | PA | USA |
| San Antonio | TX | USA |
| San Diego | CA | USA |
| Dallas | TX | USA |
| Austin | TX | USA |
Create TRUE/FALSE columns
A comparison such as state == "TX" gives TRUE or FALSE for each row. Storing it as a column makes it easy to count or filter later:
cities |> mutate(is_texas = state == "TX")
| city | state | is_texas |
|---|---|---|
| New York | NY | FALSE |
| Los Angeles | CA | FALSE |
| Chicago | IL | FALSE |
| Houston | TX | TRUE |
| Phoenix | AZ | FALSE |
| Philadelphia | PA | FALSE |
| San Antonio | TX | TRUE |
| San Diego | CA | FALSE |
| Dallas | TX | TRUE |
| Austin | TX | TRUE |
Conditions work the same way as inside filter(), so everything from the dplyr filter lesson can be used here.
mutate() with if_else()
Often you want a column that depends on a condition, like "large" or "mid-size". Use if_else(condition, value_if_true, value_if_false). This is the dplyr version of the ifelse function in base R:
cities |> mutate(size = if_else(population_2020 > 2000, "large", "mid-size"))
| city | population_2020 | size |
|---|---|---|
| New York | 8804 | large |
| Los Angeles | 3899 | large |
| Chicago | 2746 | large |
| Houston | 2305 | large |
| Phoenix | 1608 | mid-size |
| Philadelphia | 1604 | mid-size |
| San Antonio | 1435 | mid-size |
| San Diego | 1387 | mid-size |
| Dallas | 1304 | mid-size |
| Austin | 962 | mid-size |
Cities above 2,000 thousand are "large", and all the rest are "mid-size". if_else() is stricter than base ifelse(): both outcomes must be the same type, so you catch mistakes early. When you need three or more outcomes, nested if_else() calls get messy. Use dplyr case_when instead.
Use a column summary
A calculation can include a single value computed from the whole column, such as an average. That one value is reused for every row. Here is how far each city is from the average 2020 population (2605.4 thousand):
cities |> mutate(diff_from_avg = population_2020 - mean(population_2020))
| city | population_2020 | diff_from_avg |
|---|---|---|
| New York | 8804 | 6198.6 |
| Los Angeles | 3899 | 1293.6 |
| Chicago | 2746 | 140.6 |
| Houston | 2305 | -300.4 |
| Phoenix | 1608 | -997.4 |
| Philadelphia | 1604 | -1001.4 |
| San Antonio | 1435 | -1170.4 |
| San Diego | 1387 | -1218.4 |
| Dallas | 1304 | -1301.4 |
| Austin | 962 | -1643.4 |
New York is far above average, and Austin is well below it. If a column contains missing values, mean() returns NA. Use mean(x, na.rm = TRUE) in that case.
Change or remove a column
If the name on the left already exists, mutate() replaces that column rather than adding one. Make the city names upper case:
cities |> mutate(city = toupper(city))
| city | state | region |
|---|---|---|
| NEW YORK | NY | Northeast |
| LOS ANGELES | CA | West |
| CHICAGO | IL | Midwest |
| HOUSTON | TX | South |
| PHOENIX | AZ | West |
| PHILADELPHIA | PA | Northeast |
| SAN ANTONIO | TX | South |
| SAN DIEGO | CA | West |
| DALLAS | TX | South |
| AUSTIN | TX | South |
Replacing is the way to convert units, fix types, or clean text. To remove a column, set it to NULL:
cities |> mutate(region = NULL)
| city | state | population_2010 | population_2020 |
|---|---|---|---|
| New York | NY | 8175 | 8804 |
| Los Angeles | CA | 3793 | 3899 |
| Chicago | IL | 2696 | 2746 |
| Houston | TX | 2099 | 2305 |
| Phoenix | AZ | 1446 | 1608 |
| Philadelphia | PA | 1526 | 1604 |
| San Antonio | TX | 1327 | 1435 |
| San Diego | CA | 1307 | 1387 |
| Dallas | TX | 1198 | 1304 |
| Austin | TX | 790 | 962 |
For dropping columns, select(-region) is more common and reads more clearly.
Choose where the column goes
By default, new columns are added at the end. Use .before or .after to place them. Both accept a column name or position:
cities |> mutate(growth = population_2020 - population_2010, .after = city)
| city | growth | state | region | population_2010 | population_2020 |
|---|---|---|---|---|---|
| New York | 629 | NY | Northeast | 8175 | 8804 |
| Los Angeles | 106 | CA | West | 3793 | 3899 |
| Chicago | 50 | IL | Midwest | 2696 | 2746 |
| Houston | 206 | TX | South | 2099 | 2305 |
| Phoenix | 162 | AZ | West | 1446 | 1608 |
| Philadelphia | 78 | PA | Northeast | 1526 | 1604 |
| San Antonio | 108 | TX | South | 1327 | 1435 |
| San Diego | 80 | CA | West | 1307 | 1387 |
| Dallas | 106 | TX | South | 1198 | 1304 |
| Austin | 172 | TX | South | 790 | 962 |
The growth column now sits right after city. You can also write .before = population_2010, or .before = 1 to make it the first column.
Control which columns are kept
The .keep argument decides which existing columns appear in the result:
.keep = | Result |
|---|---|
"all" (default) | All original columns plus the new ones |
"used" | New columns, plus the original columns that were used to build them |
"unused" | New columns, plus original columns that were not used |
"none" | Only the new columns |
cities |> mutate(growth = population_2020 - population_2010, .keep = "used")
| population_2010 | population_2020 | growth |
|---|---|---|
| 8175 | 8804 | 629 |
| 3793 | 3899 | 106 |
| 2696 | 2746 | 50 |
| 2099 | 2305 | 206 |
| 1446 | 1608 | 162 |
| 1526 | 1604 | 78 |
| 1327 | 1435 | 108 |
| 1307 | 1387 | 80 |
| 1198 | 1304 | 106 |
| 790 | 962 | 172 |
Only the two population columns, which were used in the calculation, remain, plus growth.
transmute()
transmute() works like mutate() but keeps only the columns you create or name. It is a shortcut for mutate(..., .keep = "none"):
cities |> transmute(city, growth = population_2020 - population_2010)
| city | growth |
|---|---|
| New York | 629 |
| Los Angeles | 106 |
| Chicago | 50 |
| Houston | 206 |
| Phoenix | 162 |
| Philadelphia | 78 |
| San Antonio | 108 |
| San Diego | 80 |
| Dallas | 106 |
| Austin | 172 |
Here city is listed on its own, so it is kept. Everything not mentioned is dropped. Use transmute() when you want a small, tidy result, and mutate() when you want to keep the full table.
mutate() within groups
Normally a column summary uses the whole table. Since dplyr 1.1.0, the .by argument calculates it separately for each group. This adds the total 2020 population of each city's region:
cities |> mutate(region_total = sum(population_2020), .by = region)
| city | region | population_2020 | region_total |
|---|---|---|---|
| New York | Northeast | 8804 | 10408 |
| Los Angeles | West | 3899 | 6894 |
| Chicago | Midwest | 2746 | 2746 |
| Houston | South | 2305 | 6006 |
| Phoenix | West | 1608 | 6894 |
| Philadelphia | Northeast | 1604 | 10408 |
| San Antonio | South | 1435 | 6006 |
| San Diego | West | 1387 | 6894 |
| Dallas | South | 1304 | 6006 |
| Austin | South | 962 | 6006 |
Every row keeps its place, and each city gets the total for its own region. This differs from summarise(), which would collapse each region into a single row. Grouping is covered in dplyr group_by and summarise.
mutate() in a pipeline
Here is a realistic sequence. Which cities grew by more than 8%, and by how much? First create the growth percentage, then filter, then tidy up and sort:
cities |>
mutate(growth_pct = round((population_2020 - population_2010) / population_2010 * 100, 1)) |>
filter(growth_pct > 8) |>
select(city, state, growth_pct) |>
arrange(desc(growth_pct))
| city | state | growth_pct |
|---|---|---|
| Austin | TX | 21.8 |
| Phoenix | AZ | 11.2 |
| Houston | TX | 9.8 |
| Dallas | TX | 8.8 |
| San Antonio | TX | 8.1 |
Order matters. filter() can only use growth_pct because mutate() created it earlier in the chain. The arrange() step is covered in the dplyr arrange lesson.
dplyr mutate vs base R
| Task | dplyr | Base R |
|---|---|---|
| Add a column | mutate(cities, growth = population_2020 - population_2010) | cities$growth <- cities$population_2020 - cities$population_2010 |
| Add several columns | mutate(cities, a = ..., b = ...) | transform(cities, a = ..., b = ...) |
| Conditional column | mutate(cities, size = if_else(x > 2000, "large", "mid-size")) | cities$size <- ifelse(cities$x > 2000, "large", "mid-size") |
| Remove a column | mutate(cities, region = NULL) | cities$region <- NULL |
The base R assignment cities$growth <- ... changes the data frame in place. mutate() never does. It returns a copy, which makes pipelines safer.
Common mistakes
| Mistake | What happens | Fix |
|---|---|---|
| Not saving the result | The column shows in the output but cities is unchanged | Assign it: cities <- cities |> mutate(...) |
mutate(growth <- a - b) | You get a column with a strange name instead of growth | Use = inside mutate() |
mutate(growth = cities$a - cities$b) | Works for simple cases, but breaks with grouping or after a filter | Write the column names bare: a - b |
| Supplying a vector of the wrong length | Error that says it can't recycle a value to the table size (wording varies by version) | Give one value, or exactly one value per row |
if_else() with mixed types | Error: the true and false values cannot be combined | Make both values the same type, for example both text |
| Using a new column before it exists | Error: object not found | Create it in an earlier mutate() or earlier in the same call |
Averages come out as NA | The column has missing values | Use na.rm = TRUE |
Practice exercises
Use the five-column cities table. Try each one before opening the answer.
Exercise 1
Add a column growth that holds the 2020 population minus the 2010 population.
Show answer
cities |> mutate(growth = population_2020 - population_2010)
| city | state | region | population_2010 | population_2020 | growth |
|---|---|---|---|---|---|
| New York | NY | Northeast | 8175 | 8804 | 629 |
| Los Angeles | CA | West | 3793 | 3899 | 106 |
| Chicago | IL | Midwest | 2696 | 2746 | 50 |
| Houston | TX | South | 2099 | 2305 | 206 |
| Phoenix | AZ | West | 1446 | 1608 | 162 |
| Philadelphia | PA | Northeast | 1526 | 1604 | 78 |
| San Antonio | TX | South | 1327 | 1435 | 108 |
| San Diego | CA | West | 1307 | 1387 | 80 |
| Dallas | TX | South | 1198 | 1304 | 106 |
| Austin | TX | South | 790 | 962 | 172 |
Exercise 2
Add a column label that looks like "Austin (TX)".
Show answer
cities |> mutate(label = paste0(city, " (", state, ")"))
| city | state | label |
|---|---|---|
| New York | NY | New York (NY) |
| Los Angeles | CA | Los Angeles (CA) |
| Chicago | IL | Chicago (IL) |
| Houston | TX | Houston (TX) |
| Phoenix | AZ | Phoenix (AZ) |
| Philadelphia | PA | Philadelphia (PA) |
| San Antonio | TX | San Antonio (TX) |
| San Diego | CA | San Diego (CA) |
| Dallas | TX | Dallas (TX) |
| Austin | TX | Austin (TX) |
Exercise 3
Add a column fast_growth that says "yes" if the 2020 population is more than 8% above 2010, and "no" otherwise.
Show answer
cities |>
mutate(fast_growth = if_else(population_2020 > population_2010 * 1.08, "yes", "no"))
| city | population_2010 | population_2020 | fast_growth |
|---|---|---|---|
| New York | 8175 | 8804 | no |
| Los Angeles | 3793 | 3899 | no |
| Chicago | 2696 | 2746 | no |
| Houston | 2099 | 2305 | yes |
| Phoenix | 1446 | 1608 | yes |
| Philadelphia | 1526 | 1604 | no |
| San Antonio | 1327 | 1435 | yes |
| San Diego | 1307 | 1387 | no |
| Dallas | 1198 | 1304 | yes |
| Austin | 790 | 962 | yes |
Exercise 4
Return only city and a new growth column, using transmute().
Show answer
cities |> transmute(city, growth = population_2020 - population_2010)
| city | growth |
|---|---|
| New York | 629 |
| Los Angeles | 106 |
| Chicago | 50 |
| Houston | 206 |
| Phoenix | 162 |
| Philadelphia | 78 |
| San Antonio | 108 |
| San Diego | 80 |
| Dallas | 106 |
| Austin | 172 |
Exercise 5
Add a column region_max that holds the largest 2020 population in each city's region.
Show answer
cities |> mutate(region_max = max(population_2020), .by = region)
| city | region | population_2020 | region_max |
|---|---|---|---|
| New York | Northeast | 8804 | 8804 |
| Los Angeles | West | 3899 | 3899 |
| Chicago | Midwest | 2746 | 2746 |
| Houston | South | 2305 | 2305 |
| Phoenix | West | 1608 | 3899 |
| Philadelphia | Northeast | 1604 | 8804 |
| San Antonio | South | 1435 | 2305 |
| San Diego | West | 1387 | 3899 |
| Dallas | South | 1304 | 2305 |
| Austin | South | 962 | 2305 |
dplyr FAQ
How do I add a new column in R with dplyr?
Use mutate() with the new column name on the left of an equals sign and the calculation on the right, for example mutate(cities, growth = population_2020 - population_2010).
What is the difference between mutate() and transmute()?
mutate() keeps all existing columns and adds the new ones. transmute() keeps only the columns you create or name, and drops the rest.
How do I create multiple columns at once in dplyr?
List them in one mutate() call, separated by commas. Later columns can use earlier ones created in the same call.
Does mutate() change the original data frame?
No. It returns a new data frame. Assign the result with <- if you want to keep it.
How do I use if else inside mutate()?
Use if_else(condition, value_if_true, value_if_false) inside mutate(). For more than two outcomes, use case_when().
How do I delete a column with mutate()?
Set it to NULL, for example mutate(cities, region = NULL). The select() function with a minus sign does the same job.
Key points
mutate(new_name = calculation)adds a column. The row count stays the same.- Put several columns in one call, and later ones can use earlier ones.
- Using an existing name replaces that column. Setting a column to
NULLremoves it. - Use
if_else()for two outcomes and case_when() for more. .before,.afterand.keepcontrol column position and which columns remain.transmute()keeps only the new columns..bycomputes group-wise values without collapsing rows.- The result is a new table. Assign it with
<-to keep it.
Next, learn to sort rows with dplyr arrange. For a refresher on picking rows, see dplyr filter.