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
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
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
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.
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
Furthermore, you might want to have a look at the related articles of this website. I have released several other articles already.
- Add New Row at Specific Index Position to Data Frame
- Add New Row to Data Frame
- Unique Rows of Data Frame Based On Selected Columns
- Plot All Columns of Data Frame in R
- Add New Column to Data Frame in R
- Split Data Frame Variable into Multiple Columns
- All R Programming Examples
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.
Statistics Globe Newsletter