私はR言語でプログラミングしています。私のdata.frameオブジェクト(mydf
)のいくつかの列のフォーマット(クラス)をの文字からの因子に変更したいと思います。私はread.table()
機能でテキストファイルを読んでいるときにこれをしたくありません。どんな助けもありがとう。 R.data.frameのカラムフォーマットを文字から因子に変換
mtcars #look at this built in data set
str(mtcars) #allows you to see the classes of the variables (all numeric)
#one approach it to index with the $ sign and the as.factor function
mtcars$am <- as.factor(mtcars$am)
#another approach
mtcars[, 'cyl'] <- as.factor(mtcars[, 'cyl'])
str(mtcars) # now look at the classes
の世界へ
mydf $ myfavoritecolumn < - as.factor(mydf $ myfavoritecolumn) –
感謝を!しかし私には別の問題があります。私は、各列の名前をcol_names []という文字配列にしています。どのように私は上記のコマンド(mydf $ col_names [i])を使用することはできません動作します。 – Rasoul
data.frameはstringsAsFactorsを使ってすべての文字変数に対してこれを自動的に行う方法はありますか? –