2
我想使用字符矢量对象来轻松从xts对象中提取列数据。我只是试图将第一列名称分配给变量x
,并使用mx$x
来调用它,但它没用。有没有办法做到这一点?使用变量来调用R中的特定列?
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]' – thelatemail