【问题标题】:Subsetting an xts object containing logical values in the first column fails在第一列中对包含逻辑值的 xts 对象进行子集化失败
【发布时间】:2013-04-30 15:18:09
【问题描述】:

谁能解释一下下面的 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)])

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

【问题讨论】:

  • 这已在 R-Forge 的 r790 中修复。

标签: r xts


【解决方案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"

【讨论】:

  • 谢谢! as.POSIXct() 似乎解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 2021-06-20
  • 2017-12-01
  • 2020-10-29
  • 2014-09-01
  • 2013-05-27
  • 2014-02-02
相关资源
最近更新 更多