Language

Multilingual content from IBKR

Close Navigation
Learn more about IBKR accounts
How to Add Calculated Variables in R

How to Add Calculated Variables in R

Posted April 19, 2022 at 11:20 am
FINNSTATS

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

df
  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():

library(dplyr)

create a new variable called score1

df %>% transmute(score1 = score * 2)
   score1
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:

library(dplyr)

Transmute results are saved in a variable.

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

View the results

df
  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.

library(dplyr)

Let’s create a multiple new variables.

Group wise Correlation in R » finnstats

df %>%
  transmute(
    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: https://finnstats.com/index.php/2022/04/12/how-to-add-calculated-variables-in-r/.

Disclosure: Interactive Brokers

Information posted on IBKR Campus that is provided by third-parties does NOT constitute a recommendation that you should contract for the services of that third party. Third-party participants who contribute to IBKR Campus 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 its permission. The views expressed in this material are solely those of the author and/or FINNSTATS and Interactive Brokers 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 buy or sell any security. It should not be construed as research or investment advice or a recommendation to buy, sell or hold any security or commodity. 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.

IBKR Campus Newsletters

This website uses cookies to collect usage information in order to offer a better browsing experience. By browsing this site or by clicking on the "ACCEPT COOKIES" button you accept our Cookie Policy.