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.