在第一列中子集包含逻辑值的xts对象失败

时间:2013-04-30 15:18:10

标签: r xts

有人可以解释一下我在下面的R代码中发生了什么以及为什么?

a <- seq(as.Date('2000-01-01'),as.Date('2001-01-01'),'day')
b <- seq(as.Date('2000-01-01'),as.Date('2002-01-01'),'day')
a <- as.xts(1:NROW(a),a)
b <- as.xts(1:NROW(b),b)

NROW(b[index(a)])
d <- b>100
NROW(d[index(a)])
d <- cbind(b>100,b>100)
NROW(d[index(a)])
d <- cbind(b>100,b)
NROW(d[index(a)])
d <- cbind(b,b>100)
NROW(d[index(a)])
d <- cbind(b,b)
NROW(d[index(a)])

由于某些原因,如果xts对象在第一列中存储逻辑值,则使用日期向量对xts对象进行子集化将失败。我知道用像

这样的逻辑向量进行子集化
NROW(d[index(d) %in% index(a)])

有效,我很好奇为什么使用带日期的矢量不起作用。

1 个答案:

答案 0 :(得分:1)

看起来像个错误。逻辑运算符正在从索引中删除tzone属性,这会导致它们略有不同。例如,比较:

> attributes(.index(b))
$tzone
[1] "UTC"

$tclass
[1] "Date"

> attributes(.index(b-1))
$tzone
[1] "UTC"

$tclass
[1] "Date"

> attributes(.index(b>1))
$tzone
[1] ""

$tclass
[1] "Date"