The zoo package as a merge function which merges a set of zoo objects
result<-merge(zoo1,zoo2,...)
Assume your zoo objects are already collected in a list
# make a phony list to illustrate the situation. ( hat tip to david W for
constructing a list in a loop)
ddat <- as.list(rep("", 20))
ytd<-seq(3,14)
for(i in 1:20) {
+ ddat[[i]] <- zoo(data,ytd )
+ }
ddat
[[1]]
1 2 3 4 5 6 7 8 9 10 11 12
3 4 5 6 7 8 9 10 11 12 13 14
[[2]]
1 2 3 4 5 6 7 8 9 10 11 12
3 4 5 6 7 8 9 10 11 12 13 14
[[3]]
1 2 3 4 5 6 7 8 9 10 11 12
3 4 5 6 7 8 9 10 11 12 13 14
........
So given a list of zoo objects, if you want to create a merged zoo object
is there a way to do that.
For example, merging the first two gives you this
result<-merge(ddat[[1]],ddat[[2]])> result
ddat[[1]] ddat[[2]]
3 3 3
4 4 4
5 5 5
6 6 6
7 7 7
8 8 8
9 9 9
10 10 10
11 11 11
12 12 12
13 13 13
14 14 14
And..
result<-merge(result,ddat[[3]])> result
ddat[[1]] ddat[[2]] ddat[[3]]
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
7 7 7 7
8 8 8 8
9 9 9 9
10 10 10 10
11 11 11 11
12 12 12 12
13 13 13 13
14 14 14 14
I'm thinking that either do.call or lapply should do the trick.. or a
combination of the two. It would appear to be a straightforward
recursion applying merge to the result of merge..the looping solution is
straightforward
[[alternative HTML version deleted]]
On Mon, Jun 14, 2010 at 6:13 PM, steven mosher <moshersteven at gmail.com> wrote:> The zoo package as a merge function which merges a set of zoo objects > result<-merge(zoo1,zoo2,...) > > Assume your zoo objects are already collected in a list > > # make a phony list to illustrate the situation. ( hat tip to david W for > constructing a list in a loop) > > ddat <- as.list(rep("", 20)) > ?ytd<-seq(3,14) > ?for(i in 1:20) { > + ddat[[i]] <- zoo(data,ytd ) > + } > >merge.zoo can handle multiple zoo objects (not just two) so: do.call("merge", ddat) This also works but is a bit tedious: merge(ddat[[1]], ddat[[2]], ddat[[3]], ddat[[4]], ddat[[5]], ddat[[6]], ddat[[7]], ddat[[8]], ddat[[9]], ddat[[10]], ddat[[11]], ddat[[12]], ddat[[13]], ddat[[14]], ddat[[15]], ddat[[16]], ddat[[17]], ddat[[18]], ddat[[19]], ddat[[20]]) Also note that there is an example of using do.call with merge.zoo in the examples section of ?merge.zoo