我有兩個包含每日資料的 xts 檔案(資料只有一個月的一個日期)。第一個檔案是這樣的: - 此 xts 中的日期通常是給定月份的月末交易日期。
structure(c(-0.0329199999999997, 0.0874901766141374, 0.0545883292605231,
0.0687945180777207, 0.0550784545301166, 0.074678777314922, -0.0866534235058661,
0.161206236457536, 0.0704023794825748, 0.074691325661258), class = c("xts",
"zoo"), ret_type = "discrete", coredata_content = "discreteReturn", index = structure(c(1114732800,
1117497600, 1120089600, 1122595200, 1125446400, 1128038400, 1130716800,
1133308800, 1135900800, 1138665600), tzone = "UTC", tclass = "Date"), dim = c(10L,
1L), dimnames = list(NULL, "xts_left"))
第二個 xts 檔案是:
structure(c(0.0052512320343876, 0.00540733325225928, 0.00580017750416384,
0.005701283061746, 0.00556285472234541, 0.00561113650865441,
0.00580424365658105, 0.005816988308881, 0.00571552920344676,
0.00574088497469671, 0.00574737930337577, 0.00589584054618375,
0.00592325487612455), class = c("xts", "zoo"), .CLASS = "double", index = structure(c(1107216000,
1109635200, 1112313600, 1114905600, 1117584000, 1120176000, 1122854400,
1125532800, 1128124800, 1130803200, 1133395200, 1136073600, 1138752000
), tzone = "UTC", tclass = "Date"), dim = c(13L, 1L))
這就是我想要合并輸出的方式:從右側 xts 中選擇與左側 xts 中最接近的日期值相對應的值。例如,29-04-2005 上的值與最近的匹配,即 01-05-2005(dd-mm-yyyy 格式)。

我已經看到了一種使用帶有滾動連接的 data.table 的可能方法,但我想知道在 xts(或類似)框架中是否有方法可以做到這一點。
uj5u.com熱心網友回復:
在最后的注釋中使用 x1 和 x2 定義near給定日期 tt ,找到 x2 中最近的日期并回傳相應的資料值。然后將其應用于 x1 中的每個日期。
near <- function(tt) x2[which.min(abs(time(x2) - tt))]
x12 <- transform(x1, xts_right = sapply(time(x1), near)); x12
給予:
xts_left xts_right
2005-04-29 -0.03292000 0.005701283
2005-05-31 0.08749018 0.005562855
2005-06-30 0.05458833 0.005611137
2005-07-29 0.06879452 0.005804244
2005-08-31 0.05507845 0.005816988
2005-09-30 0.07467878 0.005715529
2005-10-31 -0.08665342 0.005740885
2005-11-30 0.16120624 0.005747379
2005-12-30 0.07040238 0.005895841
2006-01-31 0.07469133 0.005923255
在問題中顯示的示例中,最近的 x2 總是比 x1 晚,并且 x2 在 x1 之前開始。如果這些是問題的一般特征,則可以交替表示為:
transform(x1, xts_right = coredata(x2)[findInterval(time(x1), time(x2)) 1])
筆記
x1 <-
structure(c(-0.0329199999999997, 0.0874901766141374, 0.0545883292605231,
0.0687945180777207, 0.0550784545301166, 0.074678777314922, -0.0866534235058661,
0.161206236457536, 0.0704023794825748, 0.074691325661258), .Dim = c(10L,
1L), class = c("xts", "zoo"), ret_type = "discrete",
coredata_content = "discreteReturn", index = structure(c(1114732800,
1117497600, 1120089600, 1122595200, 1125446400, 1128038400, 1130716800,
1133308800, 1135900800, 1138665600), tzone = "UTC", tclass = "Date"),
.Dimnames = list(NULL, "xts_left"))
x2 <-
structure(c(0.0052512320343876, 0.00540733325225928, 0.00580017750416384,
0.005701283061746, 0.00556285472234541, 0.00561113650865441,
0.00580424365658105, 0.005816988308881, 0.00571552920344676,
0.00574088497469671, 0.00574737930337577, 0.00589584054618375,
0.00592325487612455), .Dim = c(13L, 1L), class = c("xts", "zoo"
), .CLASS = "double", index = structure(c(1107216000, 1109635200,
1112313600, 1114905600, 1117584000, 1120176000, 1122854400, 1125532800,
1128124800, 1130803200, 1133395200, 1136073600, 1138752000),
tzone = "UTC", tclass = "Date"))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/510819.html
標籤:r加入合并时间序列xts
上一篇:在python多執行緒中join()方法到底做了什么
下一篇:在R中匹配兩列中的一行
