Skip to content

Data Science Tutorials

  • Home
  • R
  • Statistics
  • Course
  • Machine Learning
  • Guest Blog
  • Contact
  • About Us
  • Toggle search form
  • The Uniform Distribution in R
    The Uniform Distribution in R R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • Arrange the rows in a specific sequence in R
    Arrange the rows in a specific sequence in R R
  • rejection region in hypothesis testing
    Rejection Region in Hypothesis Testing Statistics
  • How to Implement the Sklearn Predict Approach
    How to Implement the Sklearn Predict Approach? R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • Load Multiple Packages in R
    Load Multiple Packages in R R
  • Top Data Science Skills
    Top Data Science Skills- step by step guide Machine Learning
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 Jim No Comments on Add new calculated variables to a data frame and drop all existing variables
Tweet
Share
Share
Pin

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. 

Tweet
Share
Share
Pin
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

  • How do confidence intervals work
    How do confidence intervals work? R
  • How to do Conditional Mutate in R
    How to do Conditional Mutate in R? R
  • Convert multiple columns into a single column
    Convert multiple columns into a single column-tidyr Part4 R
  • Algorithm Classifications in Machine Learning
    Algorithm Classifications in Machine Learning Machine Learning
  • How to compare variances in R
    How to compare variances in R R
  • How to Find Optimal Clusters in R, K-means clustering is one of the most widely used clustering techniques in machine learning.
    How to Find Optimal Clusters in R? R

Leave a Reply Cancel reply

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

  • About Us
  • Contact
  • Disclaimer
  • Guest Blog
  • Privacy Policy
  • YouTube
  • Twitter
  • Facebook
  • Top 7 Skills Required to Become a Data Scientist
  • Learn Hadoop for Data Science
  • How Do Online Criminals Acquire Sensitive Data
  • Top Reasons To Learn R in 2023
  • Linear Interpolation in R-approx

Check your inbox or spam folder to confirm your subscription.

 https://www.r-bloggers.com
  • learn Hadoop for Data Science
    Learn Hadoop for Data Science Machine Learning
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • Subsetting with multiple conditions in R
    Subsetting with multiple conditions in R R
  • Rounded corner bar plot in R
    How to make a rounded corner bar plot in R? R
  • How to Standardize Data in R
    How to Standardize Data in R? R
  • Top 10 Data Visualisation Tools
    Top 10 Data Visualisation Tools Every Data Science Enthusiast Must Know Course
  • How do confidence intervals work
    How do confidence intervals work? R
  • How to Recode Values in R
    How to Recode Values in R R

Copyright © 2023 Data Science Tutorials.

Powered by PressBook News WordPress theme