小编sh2*_*657的帖子

使用变量调用R中的特定列?

我想使用字符向量对象轻松地从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
Run Code Online (Sandbox Code Playgroud)

r xts

2
推荐指数
1
解决办法
1626
查看次数

标签 统计

r ×1

xts ×1