Re: [R] A zoo related question

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Wed, 06 Apr 2011 15:33:45 -0400

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

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Wed 06 Apr 2011 - 19:36:29 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 06 Apr 2011 - 20:00:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive