You can use the transmute() function in R to add new calculated variables to a data frame and drop all existing variables.
This function uses the following basic syntax:
df %>% transmute(var_new = var1 * 2)
In this example, a new variable called var_new will be created by multiplying an existing variable called var1 by 2.
The following examples show how to use the transmute() function with the following data frame in R:
#create data frame
df frame(team=c('A', 'B', 'C', 'D', 'E'),
points=c(99, 90, 86, 88, 95),
assists=c(33, 28, 31, 39, 34),
rebounds=c(30, 28, 24, 24, 28))
#view data frame
df
team points assists rebounds
1 A 99 33 30
2 B 90 28 28
3 C 86 31 24
4 D 88 39 24
5 E 95 34 28
Example 1: Use transmute() to Create One New Variable
The following code shows how to use transmute() to create one new variable:
library(dplyr)
#create new variable called points2
df %>% transmute(points2 = points * 2)
points2
1 198
2 180
3 172
4 176
5 190
The values of points2 are equal to the original values in the points column multiplied by two.
Note that the transmute() function doesn’t actually modify the original data frame.
To save the results of the transmute() function in a new data frame, you must store them in a variable:
library(dplyr)
#store results of transmute in variable
df_points2 % transmute(points2 = points * 2)
#view results
df_points2
points2
1 198
2 180
3 172
4 176
5 190
The results of transmute() are now stored in a new data frame.
Example 2: Use transmute() to Create Multiple New Variables
The following code shows how to use transmute() to create multiple new variables from existing variables:
library(dplyr)
#create multiple new variables
df %>%
transmute(
points2 = points * 2,
rebounds_squared = rebounds^2,
assists_half = assists / 2,
team_name= paste0('team_', team)
)
points2 rebounds_squared assists_half team_name
1 198 900 16.5 team_A
2 180 784 14.0 team_B
3 172 576 15.5 team_C
4 176 576 19.5 team_D
5 190 784 17.0 team_E
Notice that four new variables have been created.
Additional Resources
The following tutorials explain how to perform other common operations in R:
How to Use the relocate() Function in dplyr
How to Use the slice() Function in dplyr
How to Filter by Row Number in dplyr