[R] biding rows while merging at the same time

Dimitri Liakhovitski dimitri.liakhovitski at gmail.com
Wed Nov 3 22:34:30 CET 2010


Hello!

I have 2 data frames like this (well, actually, I have 200 of them):

df1<-data.frame(location=c("loc 1","loc 2","loc
3"),date=c("1/1/2010","1/1/2010","1/1/2010"), a=1:3,b=11:13,c=111:113)
df2<-data.frame(location=c("loc 1","loc 2","loc
3"),date=c("2/1/2010","2/1/2010","2/1/2010"),
a=4:6,c=114:116,d=c(1,11,111))
(df1)
(df2)

I am trying to just rbind them, which is impossible, because not every
column is present in every data frame.
I can't merge them -
merge(df1,df2,by.x="location",by.y="location",all.x=T,all.y=T) -
because it kinda cbinds them.
What I need is something that looks like this:

location   date    a  b      c      d
loc 1   1/1/2010  1  11   111   NA
loc 2   1/1/2010  2  12   112   NA
loc 3   1/1/2010  3  13   113   NA
loc 1   2/1/2010  3  NA  114  1
loc 2   2/1/2010  5  NA  115  11
loc 3   2/1/2010  6  NA  116   111

Thanks a lot for your suggestions!

-- 
Dimitri Liakhovitski
Ninah Consulting
www.ninah.com



More information about the R-help mailing list