2
我需要從xts對象a
中提取子集,該子集包含來自另一個xts對象的所有日期,b
以及每個日期的相鄰日期集b
。相鄰日期可能是b
中的每個日期前的n
日期和之後的k
日期。修改了其他xts對象索引的子集xts對象
例如:
a <- structure(c(9L, 10L, 11L, 15L, 18L, 12L, 13L, 18L, 19L, 19L, 22L, 25L),
.Dim = c(12L, 1L), index = structure(c(951696000, 951868800, 951955200,
952041600, 952128000, 952214400, 952300800, 952387200, 952473600, 952560000,
952646400, 952732800), tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"),
.indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
b <- structure(1:2, .Dim = c(2L, 1L), index = structure(c(952041600, 952560000),
tzone = "UTC", tclass = "Date"), class = c("xts", "zoo"), .indexCLASS = "Date",
tclass = "Date", .indexTZ = "UTC", tzone = "UTC")
n <- 2
k <- 1
那麼輸出XTS對象,o
,應該是:
o <- structure(c(10L, 11L, 15L, 18L, 18L, 19L, 19L, 22L), .Dim = c(8L, 1L),
index = structure(c(951868800, 951955200, 952041600, 952128000, 952387200,
952473600, 952560000, 952646400), tzone = "UTC", tclass = "Date"),
class = c("xts", "zoo"), .indexCLASS = "Date", tclass = "Date",
.indexTZ = "UTC", tzone = "UTC")
我得到在b
每個日期,然後將2個前日期和1跟隨日期。我知道,例如通過服用:
a[index(b)]
我在b
得到日期。但我無法找到一種方式(可能有效!)來選擇他們旁邊的日期。