[R] merge data frames with same column names of different lengths and missing values

Dieter Menne dieter.menne at menne-biomed.de
Sun Mar 8 10:37:02 CET 2009


Steven Lubitz <slubitz1 <at> yahoo.com> writes:

> Thank you - this is very helpful. However I realized that with my real data
sets (not the example I have here),
> I also have different numbers of columns in each data frame. rbind doesn't
seem to like this. Here's a
> modified example:
> 
> x <- data.frame(item1=c(NA,NA,3,4,5), item2=c(1,NA,NA,4,5),
item3=c(NA,2,NA,4,NA), id=1:5)
> y <- data.frame(item1=c(NA,2,NA,4,5,6), item2=c(NA,NA,3,4,5,NA), id=1:6)
> 
> rbind(x,y)

You should add dummy <NA> variables to each partial data frame
such that they look the same, and do the rbind later.

Dieter




More information about the R-help mailing list