joshuaulrich / xts

Extensible time series class that provides uniform handling of many R time series classes by extending zoo.
http://joshuaulrich.github.io/xts/
GNU General Public License v2.0
219 stars 70 forks source link

period.apply() returns unusual result when function returns more than one row #382

Open joshuaulrich opened 1 year ago

joshuaulrich commented 1 year ago

Taken from this stackoverflow question. period.apply() returns a matrix with one column per row when the function returns multiple rows per period. But it should return the same result as do.call(rbind, split(x, "[period"])). For example:

dates <- seq.Date(from = as.Date("2010-01-01", format = "%Y-%m-%d"),
                  to = as.Date("2013-12-01", format = "%Y-%m-%d"), by = "month")

x <- xts(cbind(seq_along(dates), rev(seq_along(dates))), dates)

# this should be the same as do.call(rbind, split(x, "years"))
apply.yearly(x, cumsum)
##              [,1]   [,2]   [,3]   [,4]   [,5]   [,6]   [,7]   [,8]   [,9]  [,10]  [,11]  [,12]
## 2010-12-01      1      3      6     10     15     21     28     36     45     55     66     78
## 2011-12-01     13     27     42     58     75     93    112    132    153    175    198    222
## 2012-12-01     25     51     78    106    135    165    196    228    261    295    330    366
## 2013-12-01     37     75    114    154    195    237    280    324    369    415    462    510
##             [,13]  [,14]  [,15]  [,16]  [,17]  [,18]  [,19]  [,20]  [,21]  [,22]  [,23]  [,24]
## 2010-12-01     48     95    141    186    230    273    315    356    396    435    473    510
## 2011-12-01     36     71    105    138    170    201    231    260    288    315    341    366
## 2012-12-01     24     47     69     90    110    129    147    164    180    195    209    222
## 2013-12-01     12     23     33     42     50     57     63     68     72     75     77     78

The result should have the same number of observations as the input, x.

##               [,1]   [,2]
## 2010-01-01       1     48
## 2010-02-01       3     95
## 2010-03-01       6    141
## 2010-04-01      10    186
## 2010-05-01      15    230
## 2010-06-01      21    273
## 2010-07-01      28    315
## 2010-08-01      36    356
## 2010-09-01      45    396
## 2010-10-01      55    435
##        ...
## 2013-03-01     114     33
## 2013-04-01     154     42
## 2013-05-01     195     50
## 2013-06-01     237     57
## 2013-07-01     280     63
## 2013-08-01     324     68
## 2013-09-01     369     72
## 2013-10-01     415     75
## 2013-11-01     462     77
## 2013-12-01     510     78

Scenarios to test: