我有以下数据:
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")))
如何将其拆分为一个包含3个数据帧的列表,每个数据帧包含5行?我还需要代码独立于数据的行数。
答案 0 :(得分:4)
您可以创建如下的帮助函数:
groupMaker <- function(x, y) 0:(x-1) %/% y
然后,您可以split
对象:
split(xtsdata, groupMaker(nrow(xtsdata), 5))
这里的groupMaker
函数基本上可以让你方便地创建组,即使它们在没有余数的情况下可能无法被整除:
groupMaker(15, 5)
# [1] 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2
groupMaker(13, 5)
# [1] 0 0 0 0 0 1 1 1 1 1 2 2 2
定义groupMaker
的其他可能方法是使用rep
或gl
,如下所示:
groupMaker_2 <- function(x, y) gl(ceiling(x/y), y)[seq_len(x)]
groupMaker_3 <- function(x, y) rep(1:ceiling(x/y), each = y, length.out = x)
我将此作为练习让读者确定哪个是最有效的(我怀疑一般性能会有任何重大差异)。