gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:27:58 24 4
gpt4 key购买 nike

我想使用字符向量对象轻松地从 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]:

R> mx[,x]
good
2016-07-27 -0.4565496
2016-07-28 1.7436667
2016-07-29 -0.9803478
2016-07-30 -1.1954349
2016-07-31 -0.7583871
2016-08-01 -0.2496221
2016-08-02 1.6043962
2016-08-03 0.8236225
2016-08-04 -0.5089324
2016-08-05 1.1036047

关于r - 使用变量调用 R 中的特定列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38600382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com