[R] Zoo rolling window with increasing window size
Andrija Djurovic
djandrija at gmail.com
Thu Aug 10 20:43:53 CEST 2017
Something like this?
set.seed(123)
y <- rnorm(20)
sapply(1:length(y), function(x) sum(y[1:x]))
or this, depending what is the output of your custom function
lapply(1:length(y), function(x) sum(y[1:x]))
On Thu, Aug 10, 2017 at 8:39 PM, Christofer Bogaso <
bogaso.christofer at gmail.com> wrote:
> Hi Joshua, thanks for your prompt reply. However as I said, sum()
> function I used here just for demonstrating the problem, I have other
> custom function to implement, not necessarily sum()
>
> I am looking for a generic solution for above problem.
>
> Any better idea? Thanks,
>
> On Fri, Aug 11, 2017 at 12:04 AM, Joshua Ulrich <josh.m.ulrich at gmail.com>
> wrote:
> > Use a `width` of integer index locations. And you likely want =
> > "right" (or rollapplyr(), as I used).
> >
> > R> set.seed(21)
> > R> x <- rnorm(10)
> > R> rs <- rollapplyr(x, seq_along(x), sum)
> > R> cs <- cumsum(x)
> > R> identical(rs, cs)
> > [1] TRUE
> >
> >
> > On Thu, Aug 10, 2017 at 1:28 PM, Christofer Bogaso
> > <bogaso.christofer at gmail.com> wrote:
> >> 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.
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> >> 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.
> >
> >
> >
> > --
> > Joshua Ulrich | about.me/joshuaulrich
> > FOSS Trading | www.fosstrading.com
> > R/Finance 2017 | www.rinfinance.com
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list