How to split an xts object in multiple ways?

2019-09-19 04:00发布

问题:

I have the following data:

xtsdata <- structure(c(0.44696179, 0.395227931, 0.477439822, 0.295309508, 
  0.712614891, 0.689317114, 0.599395023, 0.610971864, 0.337625508, 0.529290134,
  0.596002106, 0.412324483, 0.244831259, 0.443123542, 0.484748065, 0.686165972,
  0.711764909, 0.604578061, 0.42144923, 0.669898641, 0.735845192, 0.592157589,
  0.81714156, 0.380346873, 0.684386001, 0.672967504, 0.508142689, 0.244274776,
  0.548213564, 0.417804342, 0.612475603, 0.665148957, 0.756447435, 0.582448567,
  1, 1, 1, 1, 1, 1, 0.71708817, 0.528262036, 0.597354154, 0.886971243, 0.624771744,
  0.498557661, 0.382554107, 0.464373083, 0.425888914, 0.747806533, 0.788271626,
  0.407617084, 0.784747938, 0.466987506, 0.554976586, 0.621751352, 0.501173993,
  0.323827823, 0.659625721, 0.502665703, 0.626577183, 0.458883576, 0.572507952,
  0.388946538, 0.897384403, 0.784054708, 0.652210478, 0.850226608, 0.514172118,
  0.780114865, 0.710307692, 0.714749488, 0.248817293, 0.576462902, 0.690210031),
  class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC",
  tzone = "UTC", index = structure(c(1288828800, 1288915200, 1289174400, 1289260800,
  1289347200, 1289433600, 1289520000, 1289779200, 1289865600, 1289952000,
  1290038400, 1290124800, 1290384000, 1290470400, 1290556800), tzone = "UTC",
  tclass = "Date"), .Dim = c(15L, 5L), .Dimnames = list(NULL, c("Stock1", "Stock10",
  "Stock100", "Stock101", "Stock102")))

I can split this by week like so:

xtslist <- split(xtsdata, f="weeks")

and that outputs a list of groups of 5 days.

The first thing I would like to know is how can I use the split or split.xts function to determine my own interval (for example 3).

Second, I need a function which would split xtsdata using a window of 10 but an interval of 5. So the first element in the list would contain the first 5 periods, the second would contain periods 0 to 10, and the third element will contain periods 5 to 15.

标签: r split xts