[R] Zoo rolling window with increasing window size

Christofer Bogaso bogaso.christofer at gmail.com
Thu Aug 10 20:28:45 CEST 2017


Hi again,

I am wondering there is any function for 'zoo' time series, where I
can apply a user defined function rolling window basis, wherein window
size is ever increasing i.e. not fixed. For example, let say I have
below user defined function and a zoo time series :

> library(zoo)

> UDF = function(x) sum(x)

> TS = zoo(rnorm(10), seq(as.Date('2017-01-01'), as.Date('2017-01-10'), by = '1 day'))

>

Now I want to apply UDF (this can be any custom function, however here
I put it just quick example) rolling window basis like :

1st data point = 1st data point of TS
2nd data point = sum of 1st and 2nd data points of TS
3rd data point = sum of 1st 2nd and 3rd data points of TS

so on

I am aware of the rollapply() function from zoo, however, appears like
it is only for fixed window size.

Appreciate any pointer how to achieve above strategy of implementing
rolling calculation based on increased window size.

Thanks for your time.



More information about the R-help mailing list