[R] Merging data frames one of which is NULL

Phil Spector spector at stat.berkeley.edu
Tue Nov 9 18:36:37 CET 2010

Dimitri -
    Usually the easiest way to solve problems like this
is to put all the dataframes in a list, and then use
the Reduce() function to merge them all together at the
end.  You don't give many details about how the data frames
are constructed, so it's hard to be specific about the
best way to put them in a list, but this short 
example should give you an idea of what I'm talking about:

> x<-data.frame(a=1,b=2,c=3)
> y<-data.frame(a=10,b=20,d=30)
> z<-data.frame(a=12,b=19,f=25)
> a<-data.frame(a=9,b=10,g=15)
> Reduce(function(x,y)merge(x,y,all=TRUE),list(x,y,z,a))
    a  b  c  d  f  g
1  1  2  3 NA NA NA
2  9 10 NA NA NA 15
3 10 20 NA 30 NA NA
4 12 19 NA NA 25 NA

Hope this helps.
 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu

On Tue, 9 Nov 2010, Dimitri Liakhovitski wrote:

> Hello!
> I am running a loop. The result of each run of the loop is a data
> frame. I am merging all the data frames.
> For exampe:
> The dataframe from run 1:
> x<-data.frame(a=1,b=2,c=3)
> The dataframe from run 2:
> y<-data.frame(a=10,b=20,d=30)
> What I want to get is:
> merge(x,y,all.x=T,all.y=T)
> Then I want to merge it with the output of the 3rd run, etc.
> Unfortunately, I can't create the placeholder for the overall resutls
> BEFORE I run the loop because I don't even know how many columns I'll
> end up with - after merging all the data frames.
> I was thinking of creating an empty list:
> first<-NULL
> ...and then updating it during each run by merging it with the data
> frame that is the output of the run. However, when I try to merge the
> empty list with any non-empty data frame - it ends up empty:
> merge(first,a,,all.x=T,all.y=T)
> Is there a way to make it merge while keeping everything?
> Thanks a lot!
> -- 
> Dimitri Liakhovitski
> Ninah Consulting
> www.ninah.com
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list