LoginSignup
14
18

More than 5 years have passed since last update.

R 複数列の型変換をまとめて行う方法

Posted at

複数列の型変換をまとめて行う方法

lapplyは、与えた複数の列に対してある関数をまとめて実行する関数です。

1つの変数の型をfactorに変換する場合

convert.R
df$var_1<-as.factor(df$var_1)

複数列の変数の型をまとめてfactorに変換する場合

以下は、データフレーム全てを対象とする場合です

convert2.R
n<-ncol(df)
ix<-1:n
df.2<-lapply(df[ix],as.factor)
#確認
str(df.2)
14
18
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
18