簡體   English   中英

如何按日期從xts對象列表中分離出xts數據並創建一個列表?

[英]How to separate a xts data by dates from a list of xts objects and create a list?

在這里澄清我的問題是一個可重現的示例:

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

任何幫助將不勝感激。 謝謝!

> 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

為了將來參考我曾經split的JoshuaUlrich建議創建一個季度list數據。

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

使用split而不是split.xts ,產生相同的結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM