3
this =
  structure(c(-0.012, -0.028, -0.044, -0.033, -0.039, -0.042), .Dim = c(3L, 2L),
  .Dimnames = list(NULL, c("one", "two")), index = structure(
  c(1313643600, 1313730000, 1313816400), tzone = "", tclass = "Date"),
  .indexCLASS = "Date", .indexTZ = "", class = c("xts", "zoo"))

m1=last(this$one) - last(this$two)
m2=first(last(this$one,n=2)) - first(last(this$two,n=2))

m1 > 0 #returns a TRUE OR FALSE
m1 > m2 #breaks

我知道我可以coredata用来提取然后比较。我不确定这是否是错误。比较工作似乎并不一致,甚至数学运算符在 xts 对象上工作得很好,但是将一个 xts 与另一个进行比较失败。

4

2 回答 2

5

xts logical operators work just like xts math operators. If both arguments are xts objects, the index values for both arguments have to match. In your case, m1 and m2 have different index values.

m1 > 0
#              one
# 2011-08-20 FALSE
m1 > m2
#     [,1]
m1
#               one
# 2011-08-20 -0.002
m2
#              one
# 2011-08-19 0.011
于 2011-08-17T18:22:17.697 回答
5

这与一般的时间序列一致。您无法比较(或执行任何操作)来自不同时间段的值。xts 实际上可以防止无法自然发生的行为。如果您需要将一个时期与另一个时期进行比较,则需要通过coredata()或使用lag()运算符强制。

于 2011-08-17T20:15:00.253 回答