xts comparison to another xts object does not work

Deadly 提交于 2019-12-01 06:08:11

问题


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.


回答1:


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



回答2:


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!