19
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

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

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

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)
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
19
Help us understand the problem. What are the problem?