[R] A zoo related question

Gabor Grothendieck ggrothendieck at gmail.com
Wed Apr 6 21:33:45 CEST 2011


On Wed, Apr 6, 2011 at 3:40 PM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Dear all, please consider my following workbook:
>
>
>
> library(zoo)
>
> lis1 <- vector('list', length = 2)
>
> lis2 <- vector('list', length = 2)
>
> lis1[[1]] <- zooreg(rnorm(20), start = as.Date("2010-01-01"), frequency = 1)
>
> lis1[[2]] <- zooreg(rnorm(20), start = as.yearmon("2010-01-01"), frequency =
> 12)
>
>
>
> lis2[[1]] <- matrix(1:40, 20)
>
> lis2[[2]] <- matrix(41:80, 20)
>
>
>
> Now I want to make each element of 'lis2' as zoo object where the
> corresponding indices will be borrowed from 'lis1'. This means:
>
>
>
> for (i in 1:2) {
>
>                                lis2[[i]] <- zoo(lis2[[i]],
> index(lis1[[i]]))
>
>                }
>
>
>
> However is there any faster way to do that? I found that if the sizes of
> lis1 & lis2 is quite big then it takes lot of time to complete.
>

Try this:

mapply(function(mat, z) zoo(mat, time(z)), lis2, lis1, SIMPLIFY = FALSE)

or this:

mapply(zoo, lis2, lapply(lis1, time), 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