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