71

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

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Ari B. Friedman
  • 71,271
  • 35
  • 175
  • 235

4 Answers4

95

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.

IRTFM
  • 258,963
  • 21
  • 364
  • 487
44

easy enough to use the unname() function:

data.frame <- unname(data.frame)
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
Dr Nick Engerer
  • 765
  • 7
  • 10
  • 8
    I believe rbind() actually requires the columns to have names. `rbind(unname(df[, 1:2]), unname(df[, 3:4]))` results in an `Error in if (facCol[jj]) { : missing value where TRUE/FALSE needed` error for me. – Mark Egge Nov 12 '18 at 23:36
3

rbind() needs the two object names to be the same. For example, the first object names: ID Age, the next object names: ID Gender,if you want to use rbind(), it will print out:

names do not match previous names

Draken
  • 3,134
  • 13
  • 34
  • 54
J Recaido
  • 31
  • 1
2

Use code as follows:

mylist <- lapply(pressure, function(i)read.xlsx(i,colNames = FALSE))#
mydata <- do.call('rbind',mylist)#
Paul Roub
  • 36,322
  • 27
  • 84
  • 93