Skip to content

Data Science Tutorials

For Data Science Learners

  • Maximizing Model Accuracy with Train-Test Splits in Machine Learning R
  • R Percentage by Group Calculation
    R Percentage by Group Calculation R
  • Psychological Experimentation Software
    Psychological Experimentation Software: OpenSesame Opensesame
  • Multiple regression model in R R
  • Survival Plot in R
    How to Perform a Log Rank Test in R R
  • Extract values from vector in R: dplyr R
  • Random Forest Machine Learning
    Random Forest Machine Learning Introduction R
  • Radar plot in R
    How to create Radar Plot in R-ggradar R
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

Posted on July 18July 17 By Admin No Comments on 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.

Check your inbox or spam folder to confirm your subscription.

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. 

R Tags:dplyr

Post navigation

Previous Post: How to apply a transformation to multiple columns in R?
Next Post: How to Use Spread Function in R?-tidyr Part1

Related Posts

  • Credit Card Fraud detection in R
    Credit Card Fraud Detection in R R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • how to draw heatmap in r
    How to draw heatmap in r: Quick and Easy way R
  • How to Use the Multinomial Distribution in R
    How to Use the Multinomial Distribution in R? R
  • How to create contingency tables in R
    How to create contingency tables in R? R
  • Type II Errors in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Maximizing Model Accuracy with Train-Test Splits in Machine Learning
  • Type II Errors in R
  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Ogive Graph in R
    Ogive Graph in R R
  • ggpairs in R
    ggpairs in R R
  • Count Observations by Group in R
    Count Observations by Group in R R
  • How to Load the Analysis ToolPak in Excel
    How to Load the Analysis ToolPak in Excel Excel
  • Two-Way ANOVA Example in R
    How to perform a one-sample t-test in R? R
  • Group By Sum in R
    Group By Sum in R R
  • Subset rows based on their integer locations
    Subset rows based on their integer locations-slice in R R
  • How to create summary table in R
    How to create summary table in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme