Add New Variable to Data Frame Based On Other Columns in R (2 Examples)

 

This tutorial illustrates how to create a new variable based on existing columns of a data frame in R.

The post looks as follows:

Here’s the step-by-step process…

 

Creation of Example Data

Have a look at the example data below:

data <- data.frame(x1 = 3:8,                       # Create example data
                   x2 = c(3, 1, 3, 4, 2, 5))
data                                               # Print example data

 

table 1 data frame add new variable data frame based on other columns r

 

Have a look at the previous table. It shows that our example data has six rows and two variables.

 

Example 1: Create New Variable Based On Other Columns Using $ Operator

In Example 1, I’ll illustrate how to append a new column to a data frame using other columns by applying the $ operator in R.

More precisely, we create a new variable that contains the sum of the first and of the second column.

Have a look at the following R code and its output:

data_new1 <- data                                  # Duplicate example data
data_new1$x3 <- data_new1$x1 + data_new1$x2        # Add new column
data_new1                                          # Print updated data

 

table 2 data frame add new variable data frame based on other columns r

 

In Table 2 it is shown that we have created a new data frame consisting of one more variable. This new variable consists of the sum values of our two other variables.

 

Example 2: Create New Variable Based On Other Columns Using transform() Function

An alternative to the R syntax shown in Example 1 is provided by the transform function.

In this section, I’ll illustrate how to use the transform function to add a new variable to our data frame that contains the sum of the first two variables.

data_new2 <- data                                  # Duplicate example data
data_new2 <- transform(data_new2, x3 = x1 + x2)    # Add new column
data_new2                                          # Print updated data

 

table 3 data frame add new variable data frame based on other columns r

 

The output of the previous syntax is shown in Table 3. This table contains exactly the same values as the previous table that we have created in Example 1. However, this time we have used the transform function to create a new variable based on already existing columns.

 

Video & Further Resources

Do you need further info on the R syntax of the present article? Then I recommend watching the following video of my YouTube channel. In the video, I show the R syntax of this article.

 

 

Furthermore, you might want to have a look at the related articles of this website. I have released several other articles already.

 

This article has illustrated how to add a new data frame variable according to the values of other columns in the R programming language. Let me know in the comments below, in case you have additional questions.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top