Insert New Column Between Two Data Frame Variables in R (2 Examples)
This article shows how to add new variables at a specific position of a data frame in the R programming language.
The tutorial is structured as follows:
Let’s do this:
Example Data & Add-On Packages
The following data will be used as basement for this R tutorial:
data <- data.frame(x1 = 1:5, # Example data x2 = letters[1:5], x3 = 4) data # Print data # x1 x2 x3 # 1 1 a 4 # 2 2 b 4 # 3 3 c 4 # 4 4 d 4 # 5 5 e 4
The previous output of the RStudio console shows the structure of our example data – It contains five rows and three columns with the column names x1, x2, and x3.
We also need to create a vector that we can add as new variable to our data frame:
new_col <- c(7, 1, 4, 3, 6) # New column vector new_col # Print column vector # 7 1 4 3 6
Our vector consists of five numeric values.
In the following examples, we will use functions of the tibble package. If we want to use functions of the tibble package, we also need to install and load tibble:
install.packages("tibble") # Install & load tibble package library("tibble")
Now, we are set up and can move on to the examples!
Example 1: Add Variable Using add_column Function by Index Position
This Example shows how to use the add_column function of the tibble package to insert a variable by index position. Have a look at the following application of the add_column function:
data_new1 <- add_column(data, new_col, .after = 1) # Apply add_column function by index data_new1 # Print new data # x1 new_col x2 x3 # 1 1 7 a 4 # 2 2 1 b 4 # 3 3 4 c 4 # 4 4 3 d 4 # 5 5 6 e 4
As you can see based on the previous RStudio console output, we have created a new data frame containing the columns of the original data frame and in addition our new variable new_col at the second index position (i.e. after the first index position).
Example 2: Add Variable Using add_column Function by Column Name
With the add_column command, we can also add new variables by variable names. In Example 2, I’ll show how to do that:
data_new2 <- add_column(data, new_col, .after = "x1") # Apply add_column function by name data_new2 # Print new data # x1 new_col x2 x3 # 1 1 7 a 4 # 2 2 1 b 4 # 3 3 4 c 4 # 4 4 3 d 4 # 5 5 6 e 4
The output is exactly the same as in Example 1. However, this time we inserted a new column using column names.
Video & Further Resources
I have recently published a video on my YouTube channel, which illustrates the examples of this page. You can find the video below.
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.
In addition, you could read some of the other tutorials of this website. A selection of articles is listed here:
- Add New Column to Data Frame
- Get Column Index in Data Frame by Variable Name
- Remove Data Frame Columns by Name
- Extract data.table Column as Vector Using Index Position
- The R Programming Language
At this point you should have learned how to append an additional column to a data table in the R programming language. Let me know in the comments section below, if you have further comments and/or questions.