Abiel X Reinhart <abiel.x.reinh...@jpmchase.com> writes:

> I have two identically sized matrices of data that represent time series (I 
> am storing the data in zoo objects, but the idea should apply to any matrix 
> of data). The time series in the second matrix extend further than in the 
> first matrix, and I would like to use the data in matrix 2 to extrapolate the 
> data in matrix 1. In other words, if mat1[i,j] == NA, then mat1[i,j] <- 
> mat1[i-1, j]*mat2[i,j]/mat2[i-1,j]. Of course, before we can calculate 
> mat1[i,j] we may need to calculate mat1[i-1,j], and that in turn may require 
> the computation of mat1[i-2,j], etc. This could all clearly be done with 
> loops, but I am wondering if anyone can think of a vectorized expression or 
> other efficient method that would work.

If you use tis objects and all of the columns of mat1 become NA on the same 
row, i.e.,
mat 1 looks like this:

20100101  1  2  3
20100102  4  5  6
20100103 NA NA NA
20100104 NA NA NA

then something like this should work:

mat1 <- naWindow(mat1)  ## drops the ending NA rows
rate <- growth.rate(window(mat2, start = end(mat1)))
growth.rate(mat1) <- rate

-- 
Jeff

______________________________________________
R-help@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.

Reply via email to