【发布时间】:2016-07-29 07:58:37
【问题描述】:
我想使用字符向量对象轻松地从 xts 对象中提取列数据。我只是尝试将第一列名称分配给变量x 并使用mx$x 调用它,但它没有用。有没有办法做到这一点?
library(xts)
mx <- xts(rnorm(10), Sys.Date()+1:10)
colnames(mx) <- "good"
x <- "good"
mx$x
# NULL
mx[x]
# Error in if (length(c(year, month, day, hour, min, sec)) == 6 && c(year, :
# missing value where TRUE/FALSE needed
# In addition: Warning messages:
# 1: In as_numeric(YYYY) : NAs introduced by coercion
# 2: In as_numeric(YYYY) : NAs introduced by coercion
mx[[x]]
# Error in mx[[x]] : subscript out of bounds
【问题讨论】:
-
这个必须有一个副本 - 或者可能是一个文档链接?
-
mx[,x]有效...如果您想要单列,drop(mx[,x])可能有用 -
@BenBolker - 或者只是
mx[,x,drop=TRUE]