R Merge Rows of Data Frames with Unequal Column Names (Example Code)

This article shows how to join two data frames with a different set of variable names in the R programming language.

Example Data

data(iris)                                  # Loading original iris data set
iris_1 <- iris[ , 1:3]                      # Creating first example data frame
head(iris_1)
#   Sepal.Length Sepal.Width Petal.Length
# 1          5.1         3.5          1.4
# 2          4.9         3.0          1.4
# 3          4.7         3.2          1.3
# 4          4.6         3.1          1.5
# 5          5.0         3.6          1.4
# 6          5.4         3.9          1.7
iris_2 <- iris[ , 2:5]                      # Creating second example data frame
head(iris_2)
#   Sepal.Width Petal.Length Petal.Width Species
# 1         3.5          1.4         0.2  setosa
# 2         3.0          1.4         0.2  setosa
# 3         3.2          1.3         0.2  setosa
# 4         3.1          1.5         0.2  setosa
# 5         3.6          1.4         0.2  setosa
# 6         3.9          1.7         0.4  setosa

Example: Joining Two Data Frames with Different Column Names Using dplyr Package

We need to install and load the dplyr add-on package:

install.packages("dplyr")                   # Install dplyr package
library("dplyr")                            # Load dplyr package

Next, we can use the bind_rows function to merge our data:

iris_merged <- bind_rows(iris_1, iris_2)    # Merging with bind_rows()
head(iris_merged)                           # First six rows of merged data
#     Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#   1          5.1         3.5          1.4          NA    <NA>
#   2          4.9         3.0          1.4          NA    <NA>
#   3          4.7         3.2          1.3          NA    <NA>
#   4          4.6         3.1          1.5          NA    <NA>
#   5          5.0         3.6          1.4          NA    <NA>
#   6          5.4         3.9          1.7          NA    <NA>
tail(iris_merged)                           # Bottom six rows of merged data
#     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
# 295           NA         3.3          5.7         2.5 virginica
# 296           NA         3.0          5.2         2.3 virginica
# 297           NA         2.5          5.0         1.9 virginica
# 298           NA         3.0          5.2         2.0 virginica
# 299           NA         3.4          5.4         2.3 virginica
# 300           NA         3.0          5.1         1.8 virginica

Further Resources & Related Tutorials

You may have a look at the following R tutorials. They discuss topics such as dplyr, graphics in r, indices, and variables:

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.
You need to agree with the terms to proceed

Menu
Top