9
votes

I have two large data frames(df1 and df2). I want to combine them using rbind function:

df<-rbind(df1,df2)

However, I get an error:

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

There more than 100 variables in the data frames. I know most names match. One or two names may not match. How can I find unmatched column names of df1 and df2. I will be very glad for any help. Thanks a lot.

1
colnames will give you the column names of a data.frame. If they don't match, you can set the column names of one of them and then rbind them. For instance colnames(df2)<-colnames(df1);df<-rbind(df1,df2). What's the purpose of the fill argument? - nicola
"fill" argument fills missing columns with NA nicola - oercim
Well... no. I don't know where you got that info, but there isn't any fill argument in rbind. I have the last R version (3.1.2). - nicola
Ok, nicola, I edited the question. - oercim

1 Answers

29
votes

You could use setdiff:

a <- c("a", "b", "c")
b <- c("b", "c", "d")
setdiff(a, b)
#[1] "a"
setdiff(b, a)
#[1] "d"

where a is, e.g., names(df1).