如何通过日期从xts对象列表中分离xts数据并创建列表?

时间:2014-12-15 22:14:47

标签: r list xts

这里澄清我的问题是一个可重复的例子:

ibov <- structure(c(0.029210645, -0.000172395, 0.035483633, -0.011969176,-0.007692018, 0.010634809, 0.027410321, -0.002632171, 0.000878164,0.004689955), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .CLASS = "double", class = c("xts","zoo"), index = structure(c(1041465600, 1041552000, 1041811200,1041897600, 1041984000, 1042070400, 1042156800, 1388016000, 1388102400,1388361600), tzone = "UTC", tclass = "Date"), .Dim = c(10L, 1L), .Dimnames = list(NULL, "IBOV"))
resa <- structure(c(0.010810871, 0, 0.021277441, 0, -0.021277438, 0.010695311,0.010582131, 0.038614857, -0.022989515, 0.022989514, 0.009950331,-0.006253523, -0.011131809, -0.032230311, 0.019084526, 0.027960905,-0.027960898, 0.013885075, 0.006913914, 0.001716716), class = c("xts","zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1041465600,1041552000, 1041811200, 1041897600, 1041984000, 1042070400, 1042156800,1051488000, 1051574400, 1051660800), tzone = "UTC", tclass = "Date"), .Dim = c(10L,2L), .Dimnames = list(NULL, c("ACES4", "AMBV4")))
resb <- structure(c(0.033371931, -0.066402455, 0.014815083, 0.018215437,0.01647012, -0.009275838, -0.013713674, 0, 0.020007567, 0.017603702,0.02777956, -0.001054296, -0.019169914, -0.005390845, 0.008611465,0.00641028, -0.004268949, -0.025679012, -0.08124767, 0.027822509), class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", index = structure(c(1378080000,1378166400, 1378252800, 1378339200, 1378425600, 1378684800, 1378771200,1388016000, 1388102400, 1388361600), tzone = "UTC", tclass = "Date"), .Dim = c(10L,2L), .Dimnames = list(NULL, c("AEDU3", "ALLL3")))
lst <- list(resa, resb)

如何将ibov数据分成按lst日期分类的lst列表?它应该返回这样的东西:

bova <- structure(c(0.029210645, -0.000172395, 0.035483633, -0.011969176,-0.007692018, 0.010634809, 0.027410321), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .CLASS = "double", class = c("xts","zoo"), index = structure(c(1041465600, 1041552000, 1041811200,1041897600, 1041984000, 1042070400, 1042156800), tzone = "UTC", tclass = "Date"), .Dim = c(7L, 1L), .Dimnames = list(NULL, "IBOV"))
bovb <- structure(c(-0.002632171, 0.000878164,0.004689955), .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", .CLASS = "double", class = c("xts","zoo"), index = structure(c(1388016000, 1388102400,1388361600), tzone = "UTC", tclass = "Date"), .Dim = c(3L, 1L), .Dimnames = list(NULL, "IBOV"))
newlst <- list(bova, bovb)

> newlst
[[1]]
                   IBOV
2003-01-02  0.029210645
2003-01-03 -0.000172395
2003-01-06  0.035483633
2003-01-07 -0.011969176
2003-01-08 -0.007692018
2003-01-09  0.010634809
2003-01-10  0.027410321

[[2]]
                   IBOV
2013-12-26 -0.002632171
2013-12-27  0.000878164
2013-12-30  0.004689955

任何帮助都会被贬低。谢谢!

2 个答案:

答案 0 :(得分:1)

> dt1 <- index(ibov)[index(ibov) %in% index(lst[[1]])]
> dt2 <- index(ibov)[index(ibov) %in% index(lst[[2]])]

> newlst<- list()
> newlst[[1]] <- ibov[dt1,]
> newlst[[2]] <- ibov[dt2,]
> newlst
[[1]]
                   IBOV
2003-01-02  0.029210645
2003-01-03 -0.000172395
2003-01-06  0.035483633
2003-01-07 -0.011969176
2003-01-08 -0.007692018
2003-01-09  0.010634809
2003-01-10  0.027410321

[[2]]
                   IBOV
2013-12-26 -0.002632171
2013-12-27  0.000878164
2013-12-30  0.004689955

答案 1 :(得分:0)

为了将来参考,我使用JoshuaUlrich建议的split来创建季度list数据。

ibov <- split(ibov, f = 'months', k = 3)

它与split而不是split.xts一起正常工作,产生相同的结果。