If you try to use the rbind() to row bind two data frames with mismatching column names, you will raise the error names do not match previous names. You can solve this error by renaming the columns of one of the data frames to match the other using the names() function, for example:

names(df2) <- names(df1)

You can also use the bind_rows() function from the dplyr package if you do not want to change the column names.

This tutorial will go through the error in detail and how to solve it with code examples.


What is Rbind() in R?

The rbind() function performs row binding which is a row-wise join or concatenation of two or more data frames.

Example: Names do not match previous names

Let’s look at an example of two data frames:

df1 <- data.frame(x=c(9, 3, 4, 6, 7),
y=c(8, 8, 1, 5, 2))

df2 <- data.frame(a=c(9, 1, 4, 7, 7),
b=c(1, 8, 3, 7, 2))

We want to perform row binding on the two data frames using the rbind() function:

print(rbind(df1,df2))
Error in match.names(clabs, names(xi)) : 
  names do not match previous names

The error occurs because the column names of the data frames are mismatching. The first data frame, df1, has column names x and y and the second data frame, df2, has column names a and b. We can verify that the data frames do not have identical column names by using the identical function as follows:

print(identical(names(df1), names(df2)))
[1] FALSE

The identical function returns FALSE, telling us that the column names are not identical.

Solution #1: Manually Rename Column Names

We can solve this error by manually renaming the column names of either of the data frames to match the names of the other. Let’s look at the revised code:

df1 <- data.frame(x=c(9, 3, 4, 6, 7),
y=c(8, 8, 1, 5, 2))

df2 <- data.frame(a=c(9, 1, 4, 7, 7),
b=c(1, 8, 3, 7, 2))

names(df2) <- c('x', 'y')

print(rbind(df1, df2))

Let’s run the code to get the result:

   x y
1  9 8
2  3 8
3  4 1
4  6 5
5  7 2
6  9 1
7  1 8
8  4 3
9  7 7
10 7 2

We successfully performed the row binding and have a single data frame with the rows from both data frames.

Solution #2: Automatically Rename Column Names

We can also solve this error by automatically assigning the column names of one data frame to the other. Let’s look at the revised code:

df1 <- data.frame(x=c(9, 3, 4, 6, 7),
y=c(8, 8, 1, 5, 2))

df2 <- data.frame(a=c(9, 1, 4, 7, 7),
b=c(1, 8, 3, 7, 2))

names(df2) <- names(df1)
print(rbind(df1, df2))

Let’s run the code to see the result:

   x y
1  9 8
2  3 8
3  4 1
4  6 5
5  7 2
6  9 1
7  1 8
8  4 3
9  7 7
10 7 2

We successfully performed the row binding and have a single data frame with the rows from both data frames.

Solution #3: Use bind_rows() from dplyr package

We can also solve this error by using the bind_rows() function from the dplyr package. We can install and load dplyr as follows:

install.packages("dplyr")         
library("dplyr")

Next, we will use the bind_rows() function to create a new data frame from the first two data frames. Let’s look at the revised code:

df1 <- data.frame(x=c(9, 3, 4, 6, 7),
y=c(8, 8, 1, 5, 2))

df2 <- data.frame(a=c(9, 1, 4, 7, 7),
b=c(1, 8, 3, 7, 2))

print(bind_rows(df1, df2))

Let’s run the code to see the result:

    x  y  a  b
1   9  8 NA NA
2   3  8 NA NA
3   4  1 NA NA
4   6  5 NA NA
5   7  2 NA NA
6  NA NA  9  1
7  NA NA  1  8
8  NA NA  4  3
9  NA NA  7  7
10 NA NA  7  2

The rbind() function cannot cope with mismatching column names or missing columns, whereas bind_rows() can. If there is a case of a missing column, the bind_rows() function assigns NA to the rows in the data frame where the columns were missing.

Although the bind_rows() function creates missing values in the data, this might be preferable in the case where we want to preserve column names or the content of the variables in the data frames are not the same.

Solution #4: Use rbind.fill() from plyr package

We can also solve this error by using the rbind.fill() function from the plyr package. We can install and load plyr as follows:

install.packages("plyr")         
library("plyr")
df1 <- data.frame(x=c(9, 3, 4, 6, 7),
y=c(8, 8, 1, 5, 2))

df2 <- data.frame(a=c(9, 1, 4, 7, 7),
b=c(1, 8, 3, 7, 2))

print(rbind.fill(df1, df2))

Next, we will use the rbind.fill function to create a new data frame from the first two data frames. Let’s look at the revised code:

    x  y  a  b
1   9  8 NA NA
2   3  8 NA NA
3   4  1 NA NA
4   6  5 NA NA
5   7  2 NA NA
6  NA NA  9  1
7  NA NA  1  8
8  NA NA  4  3
9  NA NA  7  7
10 NA NA  7  2

The rbind() function cannot cope with mismatching column names or missing columns, whereas rbind.fill can. If there is a case of a missing column, the rbind.fill() function assigns “NA” to the rows of columns in the data frame where that column is missing. Note that this function produces the same output as bind_rows().

Summary

Congratulations on reading to the end of this tutorial! The R Error: names do not match previous names occurs when you try to join one or more data frames using rbind() where one or more of the column names mismatch. You can either change the column names so that they are identical using names() or fill the rows of the missing columns with NA using bind_rows() or rbind.fill().

For further reading on R related errors, go to the articles:

Go to the online courses page on R to learn more about coding in R for data science and machine learning.

Have fun and happy researching!