1) Try rollapply in the zoo package:
> library(zoo) > rollapply(zoo(1:5), 3, function(x) x[2] * x[2] - x[1] * x[3]) 2 3 4 1 1 1
coredata(z) gives part of the data, i.e. c(1, 1, 1) , and time(z) gives the time part, i.e. c(2, 3, 4) .
2) Another way to do this at the zoo:
> z <- zoo(1:5) > z*z - lag(z) * lag(z,-1) 2 3 4 1 1 1
3) This last approach also works in the ts class found in the core R:
> tt <- ts(1:5) > tt * tt - lag(tt) * lag(tt, -1) Time Series: Start = 2 End = 4 Frequency = 1 [1] 1 1 1
source share