LouisChiffre
LouisChiffre

Reputation: 701

Concatenating zoo objects without a for loop

I have a function taking a date and returning a zoo object containing an intraday time series. Here is a mockup:

    getData<-function(valuationDate) 
        zoo(
            rnorm(10), 
            seq(as.POSIXlt(paste(valuationDate,"09:00")),length.out=10,by="hour")
        )

I would like to apply this function to a list of dates

    valuationDates<-seq(Sys.Date(),Sys.Date()+10,by="day")

and concatenating the resulting zoo object. If lzplywould exist, it would looks like this

    z <- lzply(valuationDates,getData)

The only solution I have found is starting with a empty zoo object and concatenating the zoo objects inside a for loop. But it's quite ugly. There must be a better way. How would you do this?

Upvotes: 0

Views: 723

Answers (1)

Roman Luštrik
Roman Luštrik

Reputation: 70643

What you want to do is create a list of -whatever-. After that, you can glue -whatever- together using rbind, cbind, c, your own function... you name it. In your case, that would be

z <- sapply(valuationDates, getData, simplify = FALSE)
do.call("rbind", z)

Upvotes: 1

Related Questions