Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rbind error: "names do not match previous names"

Tags:

r

sp

As part of a larger problem (adding a ,makeUniqueIDs argument to rbind.SpatialPolygonsDataFrame for situations when the polygon IDs are identical), I'm running into this weird message from rbind:

> do.call("rbind",xd.small) Error in match.names(clabs, names(xi)) :    names do not match previous names 
  • xd.small dput output

The only other info I could find on this was this question, which leads me to believe that rbind was at the root of the problem there also.

I can just write my own rbind-like function of course, but presumably this match.names check occurs for a reason, and I'm curious what it is.

like image 550
Ari B. Friedman Avatar asked Aug 18 '12 14:08

Ari B. Friedman


People also ask

Do call Rbind 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.

What does Rbind mean?

rbind(my_data, new_row) The name of the rbind R function stands for row-bind. The rbind function can be used to combine several vectors, matrices and/or data frames by rows.

What is the difference between Cbind and Rbind?

cbind() and rbind() both create matrices by combining several vectors of the same length. cbind() combines vectors as columns, while rbind() combines them as rows. Let's use these functions to create a matrix with the numbers 1 through 30.


2 Answers

The names (column names) of the first dataframe do not match the names of the second one. Just as the error message says.

> identical(names(xd.small[[1]]), names(xd.small[[2]]) ) [1] FALSE 

If you do not care about the names of the 3rd or 4th columns of the second df, you can coerce them to be the same:

> names(xd.small[[1]]) <- names(xd.small[[2]])  > identical(names(xd.small[[1]]), names(xd.small[[2]]) ) [1] TRUE 

Then things should proceed happily.

like image 160
IRTFM Avatar answered Sep 18 '22 21:09

IRTFM


easy enough to use the unname() function:

data.frame <- unname(data.frame) 
like image 31
Dr Nick Engerer Avatar answered Sep 20 '22 21:09

Dr Nick Engerer