Error In Match.Names

This error happens when you attempt to use the rbind() function for rowing bind two data frames, but the column names of the two data borders don’t match. This tutorial shares the exact stages you can used to troubleshoot this error.

Example: How to Solution ‘names do not match earlier names.

## Suppose We Have The Succeeding Two Data Frames In R:

#create and view the 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 the first and 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

The first data frame has the succeeding column names:

• var1
• var2

And the second data frame has the succeeding column names:

• var3
• var4

We can even used the following code to check whether the column names match between the two data frames:

### #check if column names match between two data frames

identical(names(df1), names(df2))

 FALSE

We can see that the column names remain not identical. To fix this error, we can physically rename the column names of the second data frame to contest the column names of the first 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) <- 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 get that rbind() was able to row successfully bind the two data frames later the column names matched.

Another way to fix this error would remain 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() can row bind the two data frames successfully because they share the same column names.

## Solving The Error “Names Do Not Match Previous Names” By Giving new name Columns There are two ways to handle the error message “names do not contest previous names.” The next R code shows how to fix this error by consistently the column names of our two data frames.

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

 data3 <- data2                        # Replicate datacolnames(data3) <- colnames(data1)    # Change column namesdata3                                 # 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 purpose 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 contains the values of the first and the second data frame with the flexible names of the first data frame.

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

## Solving The Error “Names Do Not Match Previous Names” Using Bind_Rows Of Dplyr A different way to deal with the error message “names do not match previous names” remains to retain all column names and add NA values for those pillar names that are not currently in both data frames.

Used for this, we can use the bind_rows purpose of the dplyr package. We first take to install and load the dplyr package:

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

Now, we can smear the bind_rows function to make an output data table that contains four columns:

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

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

## Conclusion

It is a programming problem that is easy to understand and just as easy to fix. In this case, the problem is a column name mismatch, and the rbind() function cannot handle this situation. The code needed to fix this problem is short and straightforward, making for an easy fix.