子集xts对象具有其他xts对象的修改索引

时间:2020-12-24 12:37:56

I need to extract a subset from a xts object, a, that contains all the dates from another xts object, b, plus a set of neighboring dates for each of the dates in b. The neighboring dates could be the n dates before each date in b and the k dates after.

我需要从xts对象中提取一个子集a,它包含来自另一个xts对象的所有日期,b,以及b中每个日期的一组相邻日期。相邻日期可以是b中每个日期之前的n个日期和之后的k个日期。

For example:

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

then the output xts object, o, should be:

那么输出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")

I'm getting each date in b, and then the 2 preceding dates and the 1 following date. I know that for instance by taking:

我在b中得到每个日期,然后是前两个日期和下一个日期。我知道,例如通过采取:

a[index(b)] 

I get the dates in b. But I couldn't find a way (possibly efficient!) to select the dates next to them.

我在b中得到了约会。但我找不到一种方法(可能有效率!)来选择它们旁边的日期。

1 个解决方案

#1


1  

If you literally meant "neighboring dates", you could add n and subtract k from each element of index(b):

如果你的字面意思是“相邻日期”,你可以添加n并从索引(b)的每个元素中减去k:

i <- c(0,-seq(n),seq(k))
# repeat index(b) for each value we want: 1) actual value, 2) -n, 3) +k
idx <- rep(index(b), each=length(i)) + i
o <- a[idx,]

If you actually meant "neighboring observations", you could take the output from a[index(b), which.i=TRUE], and then add n and subtract k from each element of that vector:

如果您实际上是指“邻近观察”,则可以从[index(b),which.i = TRUE]中获取输出,然后添加n并从该向量的每个元素中减去k:

i <- c(0,-seq(n),seq(k))
b.in.a <- a[index(b), which.i=TRUE]
# repeat b.in.a for each value we want: 1) actual value, 2) -n, 3) +k
idx <- rep(b.in.a, each=length(i)) + i
o <- a[idx,]

The two approaches yield the same results in your case, but they would be different if a didn't contain contiguous dates.

这两种方法在您的情况下会产生相同的结果,但如果不包含连续日期,它们会有所不同。

#1


1  

If you literally meant "neighboring dates", you could add n and subtract k from each element of index(b):

如果你的字面意思是“相邻日期”,你可以添加n并从索引(b)的每个元素中减去k:

i <- c(0,-seq(n),seq(k))
# repeat index(b) for each value we want: 1) actual value, 2) -n, 3) +k
idx <- rep(index(b), each=length(i)) + i
o <- a[idx,]

If you actually meant "neighboring observations", you could take the output from a[index(b), which.i=TRUE], and then add n and subtract k from each element of that vector:

如果您实际上是指“邻近观察”,则可以从[index(b),which.i = TRUE]中获取输出,然后添加n并从该向量的每个元素中减去k:

i <- c(0,-seq(n),seq(k))
b.in.a <- a[index(b), which.i=TRUE]
# repeat b.in.a for each value we want: 1) actual value, 2) -n, 3) +k
idx <- rep(b.in.a, each=length(i)) + i
o <- a[idx,]

The two approaches yield the same results in your case, but they would be different if a didn't contain contiguous dates.

这两种方法在您的情况下会产生相同的结果,但如果不包含连续日期,它们会有所不同。