Skip to content
Prev 4953 / 15274 Next

Vectorized rolling computation on xts series

Another approach would be to use zoo or xts's lag function to to
generate a dataframe or matrix with the current day's data and N
previous periods in a table. If your data is fairly univariate, this
shouldn't prove a problem, just do a little math and you can specify
easily how many days of data to "go back". You'd do something like
this:

starting with:

d1
d2
d3
d4
d5

use Lag (or lag, they behave differently), then t() and apply() and end up with:

d1 na na na
d2 d1 na na
d3 d2 d1 na
d4 d3 d2 d1
d5 d4 d3 d2

you can then easily run your computations in a vectored form using the
apply family of functions.
On Wed, Oct 7, 2009 at 3:30 AM, Mark Breman <breman.mark at gmail.com> wrote: