Combine Two Data Frames with Different Variables by Rows in R (Example)


In this tutorial you’ll learn how to join two data frames with a different set of variable names in R.

The table of content is structured like this:

It’s time to dive into the example…


Example Data

Have a look at the following example data:

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

The previous output of the RStudio console shows that our first example data frame has five rows and two columns. The variables of our first data frame are called x1 and x2.

Let’s create a second data frame in R:

data2 <- data.frame(x2 = LETTERS[11:15],    # Second data frame
                    x3 = 777)
#   x2  x3
# 1  K 777
# 2  L 777
# 3  M 777
# 4  N 777
# 5  O 777

The second data frame also consists of two columns. However, the column names of this data frame are different compared to the first data frame (i.e. x2 and x3 instead of x1 and x2).


Example: Merging Data Frames with Unequal Column Names Using bind_rows() of dplyr Package

The rbind function of the basic installation is not able to merge data frames with different column names.

Fortunately, the dplyr package provides this possibility.

First, we need to install and load the dplyr add-on package:

install.packages("dplyr")                   # Install & load dplyr

Now, we can apply the bind_rows function provided by the dplyr package to bin the rows of our two data frame in R:

bind_rows(data1, data2)                     # Applying bind_rows function
#    x1 x2  x3
# 1   1  a  NA
# 2   2  b  NA
# 3   3  c  NA
# 4   4  d  NA
# 5   5  e  NA
# 6  NA  K 777
# 7  NA  L 777
# 8  NA  M 777
# 9  NA  N 777
# 10 NA  O 777

As you can see based on the previous output of the RStudio console, the bind_rows function inserts NA values in columns that didn’t exist is both data frames.


Video & Further Resources

Do you need further info on the R programming code of this tutorial? Then I can recommend watching the following video of my YouTube channel. In the video, I’m explaining the examples of this article in a live session.



In addition, you may read some of the other tutorials of this homepage. I have published numerous other articles on related topics such as ggplot2, variables, graphics in r, and merging.


In summary: At this point you should have learned how to combine data frame rows where the column names are different and add the second data frame at the end or bottom of the first data frame in R programming.

Tell me about it in the comments section below, in case you have additional questions. Besides that, don’t forget to subscribe to my email newsletter for updates on the newest articles.


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.