[Solved] Error In Match.Names(Clabs, Names(xi)) : Names Do Not Match Previous Names

If you attempt to row bind two data frames with mismatched column names, the error names that do not match previous names will be raised. You can fix this error by renaming the columns in one of the data frames to match the columns in the other, for example:

names(df2) <- names(df1)

If you don’t want to change the column names, you can use the dplyr package’s bind rows() function.

This tutorial will go over the error in detail and show you how to fix it using code examples.

What is Rbind() in R?

Row binding is a row-wise join or concatenation of two or more data frames performed by the rbind() function.

What Is This Error About?

Consider the following 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))

Using the rbind() function, we want to perform row binding on the two data frames:

Error in match.names(clabs, names(xi)) : 
  names do not match previous names
  • The error occurs due to a mismatch in the column names of the data frames. The column names in the first data frame, df1, are x and y, and in the second data frame, df2, are a and b.
  • We can use the identical function to ensure that the data frames do not have identical column names:
  • The identical function returns FALSE, indicating that the column names are not the same.

How To Fix This Error?

1. Manually Rename Column Names

We can fix this problem by manually renaming the column names in each data frame to match the names in the other. Let’s take a look at the updated 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 see what happens:

   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 completed the row binding successfully and now have a single data frame with rows from both data frames.

2. Automatically Rename Column Names

This error can also be avoided by automatically assigning the column names of one data frame to the other. Let’s take a look at the updated 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 what happens:

   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 completed the row binding successfully and now have a single data frame with rows from both data frames.

3. Use Bind_Rows() From Dplyr Package

This error can also be fixed by using the bind rows() function from the dplyr package. We can install and load dplyr in the following ways:

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

The bind rows() function will then be used to create a new data frame from the first two data frames. Let’s take a look at the updated 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 what happens:

    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
  • rbind() cannot handle mismatched column names or missing columns, whereas bind rows() can. If a column is missing, the bind rows() function assigns NA to the rows in the data frame where the column was missing.
  • Although the bind rows() function generates missing values in the data, this may be preferable if we want to preserve column names or the content of the variables in the data frames differs.

4. Use Rbind.fill() From Plyr Package

We can also fix this error by using the plyr package’s rbind.fill() function. Plyr can be installed and loaded as follows:

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))

Following that, we’ll use the rbind.fill function to create a new data frame from the first two. Let’s take a look at the updated 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 handle mismatched or missing column names, whereas rbind.fill can. If a column is missing, the rbind.fill() function assigns “NA” to the rows of columns in the data frame where it is missing. It should be noted that this function yields the same results as bind rows ().

Also read:- {Solved} Error In Fix.by(by.y, y): ‘by’ Must Specify A Uniquely Valid Column

Conclusion

Congratulations on making it all the way to the end of this tutorial! When you try to join one or more data frames using rbind() and one or more of the column names do not match, the R Error: names do not match previous names occurs.

You can use names() to change the column names to be identical, or you can use bind rows() or rbind.fill to fill the rows of the missing columns with NA ().

Do check out our website for more articles on R. If you have any queries regarding this article, let us know in the comments below.

Leave a Reply

Your email address will not be published. Required fields are marked *

How To Charge Laptop In Car? [Gadgets Used] What Happens If I Reset My iPhone? Check out iPhone 14 Colors HD Pro All Full Forms Of Computer Subject