[R] Merging zoo objects

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jan 7 19:12:55 CET 2011


On Fri, Jan 7, 2011 at 1:01 PM, Pete B <Peter.Brecknock at bp.com> wrote:
>
> Hi
>
> I have n zoo objects M1, M2, M3, ... , Mn that I want to merge where n is a
> number calculated at run-time.
>
> I am struggling to find the correct syntax to do this
>
> Assuming n is calculated as 10 (for example), I have tried
>
> n = 10
> # First Effort
> alldata= merge(paste("M",rep(1:n), sep=""),all=TRUE)
>
> # Second Effort
> alldata
> =merge(noquote(toString(paste("M",rep(1:nrow(counts1)),sep=""))),all=TRUE)
>

Try this where the sapply creates a list of the objects.

library(zoo)
M1 <- zoo(11:13); M2 <- zoo(21:24); M3 <- zoo(31:35)

do.call("merge", sapply(ls(pattern = "^M"), get, simplify = FALSE))


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list