[R] Merging data frames one of which is NULL
Joshua Wiley
jwiley.psych at gmail.com
Tue Nov 9 18:11:00 CET 2010
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/
More information about the R-help
mailing list