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
I know I can use coredata
to extract and then compare. I wasn't sure if this is bug. It didn't seem consistent that comparisons work and even math operators work just fine on xts objects but comparing one xts to another fails.
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
This is consistent with time series in general. You can't compare (or do any Ops) on values from differing time periods. xts in effect guards against behavior that can't happen naturally. If you need to compare one period to another, you'll need to force via coredata() or by using the lag() operators.
来源:https://stackoverflow.com/questions/7097437/xts-comparison-to-another-xts-object-does-not-work