Add New Column to Data Frame in R

 

In this tutorial, I’ll illustrate how to add a new column to a data frame in R programming.

The table of content looks like this:

Let’s dig in.

 

Creation of Example Data

The examples of this R tutorial will be based on the following data frame:

data <- data.frame(x1 = 1:5,             # Create example data
                   x2 = letters[1:5])
data                                     # Print example data
#   x1 x2
# 1  1  a
# 2  2  b
# 3  3  c
# 4  4  d
# 5  5  e

Our example data consists of five rows and the two variables x1 and x2.

Furthermore, we have to create an example vector that we can add as new column to our data frame:

vec <- c(3, 2, 3, 2, 3)                  # Create example vector
vec                                      # Print example vector
# 3 2 3 2 3

Our example vector consists of five numeric values. Note that the number of rows of our data frame and the length of our vector are equal.

Now, let’s add the vector as new variable to our data frame…

 

Example 1: Add Column with $-Operator

In Example 1, you’ll learn how to add a new column to a data frame with the $-Operator. First, we are going to replicate our original example data:

data_1 <- data                           # Replicate example data

Now, we can add our example vector to the data frame as follows:

data_1$new_col <- vec                    # Add new column to data

Let’s print the new data frame to the RStudio console:

data_1                                   # Print new data
#   x1 x2 new_col
# 1  1  a       3
# 2  2  b       2
# 3  3  c       3
# 4  4  d       2
# 5  5  e       3

As you can see, the new data frame consists of the same variables as our input data and in addition of the new variable new_col.

 

Example 2: Add Column with Square Brackets

Example 1 has shown how to add a new variable to a data frame with the $-Operator. However, there are many different ways how to do this and in this example I’ll show you how to concatenate a vector to a data frame with square brackets.

Have a look at the following R code:

data_2 <- data                           # Replicate example data
data_2["new_col"] <- vec                 # Add new column to data

data_2 contains our original data as well as our example vector vec.

 

Example 3: Add Column with cbind Function

Another alternative for creating new variables in a data frame is the cbind function. The cbind function can be used to add columns to a data matrix as follows:

data_3 <- data                           # Replicate example data
data_3 <- cbind(data, new_col = vec)     # Add new column to data

Again, the output is a data frame consisting of our original data and a new column.

 

Video, Further Resources & Summary

Do you need more information on the contents of this article? Then you may want to have a look at the following video of my YouTube channel. In the video tutorial, I’m explaining the contents of this article in RStudio:

 

 

Furthermore, you might want to have a look at the other tutorials of my website. Some tutorials about data manipulation in R can be found below:

 

In this R tutorial you learned how to append a vector to a data frame. Don’t hesitate to let me know in the comments section, if you have further 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.


6 Comments. Leave new

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