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

Dieter Menne dieter.menne at menne-biomed.de
Sat Mar 7 09:56:39 CET 2009


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

> 
> x <- data.frame(item1=c(NA,NA,3,4,5), item2=c(1,NA,NA,4,5), 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)
> 
....
> merge(x,y,by=c("id","item1","item2"),all.x=T,all.y=T) #my rows are duplicated
and the NA values are
> retained - I instead want one row per ID
>   id item1 item2
> 1  1    NA     1
> 2  1    NA    NA
> 3  2     2    NA
> 4  2    NA    NA
> 5  3     3    NA
> 6  3    NA     3
> 7  4     4     4
> 8  5     5     5
> 9  6     6    NA
> 
I think you only got the wrong (too complex) function. Try rbind(x,y)

Dieter




More information about the R-help mailing list