How to Add Calculated Variables in R



To add new calculated variables to a data frame and remove all old variables, use the transmute() function in R.

The following is the fundamental syntax for this function.

df %>% transmute(var_new = var1 * 2)

In this example, we’ll make a new variable called var new by multiplying an existing variable called var1 by 2.

The examples below demonstrate how to use the transmute() function in R with the following data frame.

Let’s create a data frame

df <- data.frame(team=c('A1', 'B1', 'C1', 'D1', 'E1'),
                 score=c(89, 70, 76, 78, 75))

Now we can view the data frame

  team score
1   A1    89
2   B1    70
3   C1    76
4   D1    78
5   E1    75

Example 1: Create One New Variable using transmute()

The following code demonstrates how to create a new variable with transmute():


create a new variable called score1

df %>% transmute(score1 = score * 2)
1    178
2    140
3    152
4    156
5    150

The original values in the score column are multiplied by two to get the score1 values.

It’s worth noting that the transmute() function doesn’t change the original data frame.

You must save the results of the transmute() method in a variable to save them in a new data frame:


Transmute results are saved in a variable.

df$score1<- df %>% transmute(score1 = score * 2)

View the results

  team score score1
1   A1    89    178
2   B1    70    140
3   C1    76    152
4   D1    78    156
5   E1    75    150

The results of transmute() are now stored in a data frame.

Example 2: Create Multiple New Variables using transmute()

Transmute() can be used to create several new variables from existing variables, as shown in the following code.


Let’s create a multiple new variables.

Group wise Correlation in R » finnstats

df %>%
    score1 = score * 2,
    scoresquared = score^2,
    scorehalf = score / 2,
    name= paste0('team_', team)
 score1 scoresquared scorehalf    name
1    178         7921      44.5 team_A1
2    140         4900      35.0 team_B1
3    152         5776      38.0 team_C1
4    156         6084      39.0 team_D1
5    150         5625      37.5 team_E1

Four new variables have been created, as you can see.

Visit FINNSTATS for additional insight on this topic:

Disclosure: Interactive Brokers

Information posted on IBKR Traders’ Insight that is provided by third-parties and not by Interactive Brokers does NOT constitute a recommendation by Interactive Brokers that you should contract for the services of that third party. Third-party participants who contribute to IBKR Traders’ Insight are independent of Interactive Brokers and Interactive Brokers does not make any representations or warranties concerning the services offered, their past or future performance, or the accuracy of the information provided by the third party. Past performance is no guarantee of future results.

This material is from FINNSTATS and is being posted with permission from FINNSTATS. The views expressed in this material are solely those of the author and/or FINNSTATS and IBKR is not endorsing or recommending any investment or trading discussed in the material. This material is not and should not be construed as an offer to sell or the solicitation of an offer to buy any security. To the extent that this material discusses general market activity, industry or sector trends or other broad based economic or political conditions, it should not be construed as research or investment advice. To the extent that it includes references to specific securities, commodities, currencies, or other instruments, those references do not constitute a recommendation to buy, sell or hold such security. This material does not and is not intended to take into account the particular financial conditions, investment objectives or requirements of individual customers. Before acting on this material, you should consider whether it is suitable for your particular circumstances and, as necessary, seek professional advice.

In accordance with EU regulation: The statements in this document shall not be considered as an objective or independent explanation of the matters. Please note that this document (a) has not been prepared in accordance with legal requirements designed to promote the independence of investment research, and (b) is not subject to any prohibition on dealing ahead of the dissemination or publication of investment research.

Any trading symbols displayed are for illustrative purposes only and are not intended to portray recommendations.