Add new calculated variables to a data frame and drop all existing variables
Add new calculated variables to a data frame and drop all existing variables, I hope you enjoyed reading about the dplyr package magics in earlier posts, here is the last update while using dplyr package.
With R’s transmute() function, you can drop all of the existing variables and add new calculated variables to a data frame.
The basic syntax used by this function is as follows.
df %>% transmute(var_new = var1 * 2)
In this example, the existing variable var1 will be multiplied by 2 to produce a new variable called var new.
With the following data frame in R, the ensuing examples demonstrate how to utilize the transmute() function.
Let’s create a data frame
df <- data.frame(team=c('P1', 'P2', 'P3', 'P4', 'P5'), points=c(129, 110, 115, 128, 412), assists=c(313, 238, 331, 339, 234), rebounds=c(230, 128, 324, 124, 228))
Now we can view the data frame
df
team points assists rebounds 1 P1 129 313 230 2 P2 110 238 128 3 P3 115 331 324 4 P4 128 339 124 5 P5 412 234 228
Example 1: Use transmute() to Create One New Variable
One new variable can be made using transmute() by using the following code.
library(dplyr)
Let’s create a new variable called points2
df %>% transmute(mypoint = points * 2)
mypoint 1 258 2 220 3 230 4 256 5 824
The original values in the points column multiplied by two give the values of mypoint.
Note that the original data frame is not actually modified by the transmute() method.
You must store the output of the transmute() function in a variable in order to save it in a new data frame.
library(dplyr)
the transmutation’s outcomes in a variable
mypoint<- df %>% transmute(mypoint = points * 2)
Now we can view the results
mypoint mypoint 1 258 2 220 3 230 4 256 5 824
Transmute(output )’s is now kept in a fresh data frame.
Example 2: Create several new variables with transmute()
Transmute() can be used to generate numerous new variables from a single set of existing variables. See the example code below.
Let’s create multiple new variables
df %>% transmute( mypont = points * 2, rebounds_squared = rebounds^2, assists_half = assists / 2, team_name= paste0('team_', team) )
mypont rebounds_squared assists_half team_name 1 258 52900 156.5 team_P1 2 220 16384 119.0 team_P2 3 230 104976 165.5 team_P3 4 256 15376 169.5 team_P4 5 824 51984 117.0 team_P5
Four additional variables have been added, as you can see.
Have you liked this article? If you could email it to a friend or share it on Facebook, Twitter, or Linked In, I would be eternally grateful.
Please use the like buttons below to show your support. Please remember to share and comment below.