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
Never mind - I found it in "reshape" package: rbind.fill I wonder if it's still in reshape2. Dimitri On Wed, Nov 3, 2010 at 5:34 PM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> wrote:> 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 >-- Dimitri Liakhovitski Ninah Consulting www.ninah.com
Just merge(df1, df2, all = TRUE) does it, yes? Dimitri Liakhovitski wrote:> 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! >