Let us say you have your data frames in a nice list:
l <- list(
data.frame(x2=rnorm(3),x1=rnorm(3)),
data.frame(x1=rnorm(3),x2=rnorm(3),x3=rnorm(3),x4=rnorm(3),x5=rnorm(3)),
data.frame(x5=rnorm(3),x2=rnorm(3),x3=rnorm(3),x4=rnorm(3),x1=rnorm(3)),
data.frame(x5=rnorm(3),x2=rnorm(3),x3=rnorm(3),x4=rnorm(3)),
data.frame(x2=rnorm(3),x1=rnorm(3),x3=rnorm(3),x4=rnorm(3))
)
Grab the first one and (as @joran suggested) merge
all the rest to that with eg. a lucid loop:
r <- l[[1]]
for (i in 2:length(l)) {
r <- merge(r, l[[i]], all=TRUE)
}
Check out r
:
> r
x2 x3 x4 x1 x5
1 -1.72436 -0.774652 3.10001 0.23249 -1.278216
2 -1.25640 NA NA 0.32997 NA
3 -1.00652 -0.946254 1.17313 NA 2.014517
4 -0.53770 -0.466626 -0.63369 -1.48375 -1.135515
5 -0.49787 NA NA -0.34020 NA
6 -0.49704 -0.054175 0.85477 NA 0.831706
7 0.13027 0.421750 -0.18126 -0.65452 0.476576
8 0.18519 -1.006994 0.15141 0.66808 NA
9 0.33954 -0.224478 1.38596 NA 0.145807
10 0.57782 1.126430 -0.89582 0.80199 NA
11 0.59149 -0.447669 0.74855 -1.65790 0.059767
12 0.61374 0.751528 -1.93715 0.40125 -0.148243
13 0.89399 0.758481 -0.94801 0.05084 NA
14 0.94200 NA NA 0.24945 NA
15 0.99509 0.586097 -0.91455 -0.49909 0.823696
I didn't like that loop, so wrote some recursive stuff:
> merger <- function(l) lapply(2:length(l), function(x) merge(l[[x-1]], l[[x]], all=TRUE))
> while (length(l) != 1) l<-merger(l)
> l
[[1]]
x2 x1 x3 x4 x5
1 -1.72436 0.23249 -0.774652 3.10001 -1.278216
2 -1.25640 0.32997 NA NA NA
3 -1.00652 NA -0.946254 1.17313 2.014517
4 -0.53770 -1.48375 -0.466626 -0.63369 -1.135515
5 -0.49787 -0.34020 NA NA NA
6 -0.49704 NA -0.054175 0.85477 0.831706
7 0.13027 -0.65452 0.421750 -0.18126 0.476576
8 0.18519 0.66808 -1.006994 0.15141 NA
9 0.33954 NA -0.224478 1.38596 0.145807
10 0.57782 0.80199 1.126430 -0.89582 NA
11 0.59149 -1.65790 -0.447669 0.74855 0.059767
12 0.61374 0.40125 0.751528 -1.93715 -0.148243
13 0.89399 0.05084 0.758481 -0.94801 NA
14 0.94200 0.24945 NA NA NA
15 0.99509 -0.49909 0.586097 -0.91455 0.823696