How to fix Error in R: names do not match previous names

If you receive an error in R stating that names do not match previous names, it means that the column names of the data you are trying to use do not match the names of the columns used in the code you are running. To fix this, you need to make sure that the column names in the data you are using match the column names specified in the code. This can be done by renaming the columns in your data, or by editing the code to match the names of the columns in your data.


One common error you may encounter in R is:

Error in match.names(clabs, names(xi)) : 
  names do not match previous names

This error occurs when you attempt to use the rbind() function to row bind two data frames, but the column names of the two data frames don’t match.

This tutorial shares the exact steps you can use to troubleshoot this error.

Example: How to Fix ‘names do not match previous names’

Suppose we have the following two data frames in R:

#create and view first data frame
df1 <- data.frame(var1=c(1, 3, 3, 4, 5),
                  var2=c(7, 7, 8, 3, 2))

df1

  var1 var2
1    1    7
2    3    7
3    3    8
4    4    3
5    5    2

#create and view first second frame 
df2 <- data.frame(var3=c(3, 3, 6, 6, 8),
                  var4=c(1, 1, 2, 8, 9))

df2

  var3 var4
1    3    1
2    3    1
3    6    2
4    6    8
5    8    9

If we attempt to use the rbind() function to row bind these two data frames, we’ll get an error:

#attempt to row bind the two data frames
rbind(df1, df2)

Error in match.names(clabs, names(xi)) : 
  names do not match previous names

We get this error because the column names of the two data frames don’t match.

The first data frame has the following column names:

  • var1
  • var2

And the second data frame has the following column names:

  • var3
  • var4

We can even use the following code to check whether the column names are identical between the two data frames:

#check if column names are identical between two data frames
identical(names(df1), names(df2))

[1] FALSE

We can see that the column names are not identical.

#define two data frames
df1 <- data.frame(var1=c(1, 3, 3, 4, 5),
                  var2=c(7, 7, 8, 3, 2))

df2 <- data.frame(var3=c(3, 3, 6, 6, 8),
                  var4=c(1, 1, 2, 8, 9))

#rename second data frame columns
names(df2) <- c('var1', 'var2')

#row bind the two data frames
rbind(df1, df2)

   var1 var2
1     1    7
2     3    7
3     3    8
4     4    3
5     5    2
6     3    1
7     3    1
8     6    2
9     6    8
10    8    9

We can see that rbind() was able to successfully row bind the two data frames since the column names matched.

Another way to fix this error would be to use the names() function to automatically assign the column names of the first data frame to the second data frame:

#define two data frames
df1 <- data.frame(var1=c(1, 3, 3, 4, 5),
                 var2=c(7, 7, 8, 3, 2))

df2 <- data.frame(var3=c(3, 3, 6, 6, 8),
                  var4=c(1, 1, 2, 8, 9))

#rename second data frame columns
names(df2) <- names(df1)

#row bind the two data frames
rbind(df1, df2)

   var1 var2
1     1    7
2     3    7
3     3    8
4     4    3
5     5    2
6     3    1
7     3    1
8     6    2
9     6    8
10    8    9

Once again, rbind() is able to row bind the two data frames successfully because they share the same column names.

x