rolling computations in xts by month

后端 未结 3 1339
庸人自扰
庸人自扰 2021-01-14 11:27

I am familiar with the zoo function rollapply which allows you to do rolling computations on zoo or xts objects and you c

3条回答
  •  有刺的猬
    2021-01-14 12:14

    You want period.apply(), or its convenience helper apply.monthly(), both in xts.

    Example:

    R> foo <- xts(1:100, order.by=Sys.Date()+0:99)
    R> apply.monthly(foo, sum)
               [,1]
    2012-08-31  105
    2012-09-30  885
    2012-10-31 1860
    2012-11-25 2200
    R> 
    

    or equally

    R> apply.monthly(foo, quantile)
               0%   25%  50%   75% 100%
    2012-08-31  1  4.25  7.5 10.75   14
    2012-09-30 15 22.25 29.5 36.75   44
    2012-10-31 45 52.50 60.0 67.50   75
    2012-11-25 76 82.00 88.0 94.00  100
    R> 
    

    just to prove that functions returning more than one value can be used too.

提交回复
热议问题