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.

 

 

In addition, you could read some of the other tutorials of this website. A selection of articles is listed here:

 

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.

 

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