1

这一定很简单,但我似乎找不到解决方案。我需要比较Close2 个xts对象的值

我有 2 个xts对象:

>low2
low2           daco.Close
2013-07-24     6.63

>low3
low3           daco.Close
2013-07-24     2.63

但是当我尝试这样的事情时

if(low2$daco.Close < low3$daco.Close) {...}

我不断收到错误

Error in if (low2$daco.Close < low3$daco.Close) { : 
  argument is of length zero

即使当我尝试print(low2)print(low3)我得到正确的值时。

任何有关如何解决此问题的提示将不胜感激。谢谢!

编辑:以下是和的low2输出low3

> dput(low2)
structure(6.63, .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "xxx", updated = structure(1374782893.98805, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(1374451200, tzone = "UTC", tclass = "Date"), .Dim = c(1L, 
1L), .Dimnames = list(NULL, "daco.Close"))
> dput(low3)
structure(2.63, .indexCLASS = "Date", tclass = "Date", .indexTZ = "UTC", tzone = "UTC", src = "xxx", updated = structure(1374782893.98805, class = c("POSIXct", 
"POSIXt")), class = c("xts", "zoo"), index = structure(1374624000, tzone = "UTC", tclass = "Date"), .Dim = c(1L, 
1L), .Dimnames = list(NULL, "daco.Close"))
4

1 回答 1

1

xts 对象在操作完成之前按索引对齐。请注意,它low2$daco.Close + low3$daco.Close可能也不会返回您认为的内容。

这是您传递给您的if声明的内容:

> low2$daco.Close < low3$daco.Close     
     [,1]
> str(low2$daco.Close < low3$daco.Close)
An 'xts' object of zero-width

您可以coredata在一个或两个 xts 对象上使用。

low2$daco.Close < coredata(low3$daco.Close)
#           daco.Close
#2013-07-22      FALSE

或者你可以强迫他们中的一个或两个numeric

as.numeric(low2$daco.Close) < as.numeric(low3$daco.Close)
#[1] FALSE

如果它们确实具有相同的索引,那么它应该可以正常工作。

index(low3) <- index(low2)
low2$daco.Close < low3$daco.Close
#           daco.Close
#2013-07-22      FALSE
于 2013-07-25T21:30:09.627 回答