R rbind Function Error: Names don’t Match Previous Names (3 Examples)

 

In this tutorial, I’ll explain how to deal with the error message “names do not match previous names” in the R programming language.

Table of contents:

If you want to know more about these content blocks, keep reading:

 

Example Data

Consider the following example data:

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

Have a look at the previous output of the RStudio console. It shows that our first example data frame has five rows and two columns called x1 and x2.

Let’s create a second data frame:

data2 <- data.frame(y1 = 11:15,       # Create second example data
                    y2 = letters[11:15])
data2                                 # Print second example data
#   y1 y2
# 1 11  k
# 2 12  l
# 3 13  m
# 4 14  n
# 5 15  o

Our second example data frame also has five rows and two columns. However, the variables of this data frame are called y1 and y2.

 

Example 1: Reproducing the Error: names do not match previous names

Example 1 shows why the error message “names do not match previous names” can occur when applying the rbind function.

Let’s try to apply rbind to our two example data tables:

rbind(data1, data2)                   # Apply rbind function
# Error in match.names(clabs, names(xi)) : 
#   names do not match previous names

The RStudio console returns the error message “Error in match.names(clabs, names(xi)) : names do not match previous names”.

The reason for this is that the column names of the first and the second data frame are not the same.

Let’s solve this problem!

 

Example 2: Fixing the Error “names do not match previous names” by Renaming Columns

There are basically two ways how to handle the error message “names do not match previous names”. The following R code shows how to fix this error by harmonizing the column names of our two data frames.

The following R code creates a new data frame that contains of the values of data2, but with renamed column names:

data3 <- data2                        # Replicate data
colnames(data3) <- colnames(data1)    # Change column names
data3                                 # Print updated data
#   x1 x2
# 1 11  k
# 2 12  l
# 3 13  m
# 4 14  n
# 5 15  o

Now, let’s apply the rbind function to data1 and the new data frame data3:

rbind(data1, data3)                   # Apply rbind function
#    x1 x2
# 1   1  a
# 2   2  b
# 3   3  c
# 4   4  d
# 5   5  e
# 6  11  k
# 7  12  l
# 8  13  m
# 9  14  n
# 10 15  o

The final output matrix consists of the values of the first and the second data frame with the variable names of the first data frame.

Note that this method should only be applied in case the content of the variables of the two data frames is the same (i.e. x1 = y1 and x2 = y2). Otherwise, the output data might be theoretically flawed.

 

Example 3: Fixing the Error “names do not match previous names” Using bind_rows of dplyr

Another way to deal with the error message “names do not match previous names” is to retain all column names and insert NA values for those column names that are not existing in both data frames.

For this, we can use the bind_rows function of the dplyr package. We first have to install and load the dplyr package:

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

Now, we can apply the bind_rows function to create an output data table that consists of four columns:

bind_rows(data1, data2)               # Apply bind_rows function
#    x1   x2 y1   y2
# 1   1    a NA <NA>
# 2   2    b NA <NA>
# 3   3    c NA <NA>
# 4   4    d NA <NA>
# 5   5    e NA <NA>
# 6  NA <NA> 11    k
# 7  NA <NA> 12    l
# 8  NA <NA> 13    m
# 9  NA <NA> 14    n
# 10 NA <NA> 15    o

This method creates missing values in your data. However, it might still be the preferred alternative in case the content of the variables of both data frames is not the same.

Note that another possibility to merge our data frames would be to use the rbind.fill function of the plyr package. However, this function would produce the same output as shown in this example.

 

Video, Further Resources & Summary

Have a look at the following video of my YouTube channel. In the video, I’m illustrating the contents of this article.

 

The YouTube video will be added soon.

 

In addition, you might want to have a look at the related tutorials of this homepage. I have published numerous tutorials about errors and warnings in R already.

 

You learned in this tutorial how to solve the error of the rbind function “names do not match previous names” in the R programming language. In case you have any further comments or questions, please let me know in the comments. Besides that, don’t forget to subscribe to my email newsletter in order to get updates on new articles.

 

Subscribe to my free statistics 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.

Menu
Top