rbind error: "names do not match previous names"
Asked Answered
E

4

72

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.

Etymon answered 18/8, 2012 at 14:41 Comment(0)
C
97

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.

Cormorant answered 18/8, 2012 at 14:49 Comment(8)
D'oh. I'd checked them by eye with lapply(xd,names) but didn't notice the numbers were different.Etymon
maybe the smartbind from gtools package could be useful.Faydra
@TylerRinker That's a cool function. I'll file it away for future use, but for this I'd rather stay with base R.Etymon
I didn't notice the subtle differences the first time I looked either, and wondered if there were some non-printing character that snuck in past dput.Cormorant
rbind.fill from plyr is faster than smartbindBluing
I'm getting this error with the identical test = TRUEStrick
I don't understand what you are saying. I just repeated my process and I get the same results as I did 4 years ago.Cormorant
Very clean answer. Thanks.Gramicidin
D
44

easy enough to use the unname() function:

data.frame <- unname(data.frame)
Decade answered 16/1, 2014 at 10:51 Comment(1)
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.Vitriol
L
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

Laux answered 21/6, 2016 at 8:26 Comment(0)
D
2

Use code as follows:

mylist <- lapply(pressure, function(i)read.xlsx(i,colNames = FALSE))#
mydata <- do.call('rbind',mylist)#
Deviant answered 3/8, 2017 at 15:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.