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
Hi Dimitri, I have some doubts whether storing the results of a loop in a data frame and merging it with every run is the most efficient way of doing things, but I do not know your situation. This does what you want, I believe, but I suspect it could be quite slow. I worked around the placeholder issue using an if statement. HTH, Josh for (i in 1:10) { x <- data.frame(a = 1, b = 2, c = i) if (i == 1) { y <- x } else { y <- merge(x, y, all.x = TRUE, all.y = TRUE) } } On Tue, Nov 9, 2010 at 8:42 AM, Dimitri Liakhovitski <dimitri.liakhovitski at gmail.com> 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
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. >