LoginSignup
7

More than 5 years have passed since last update.

posted at

【R】dummiesパッケージでダミー変数を生成する

過去に発表した内容の補足としてdummy.data.frameのオプションについてのメモ。

require(dummies)

data("CO2")
str(CO2)  # データの確認
CO2データは以下のように順序付き因子、因子、数値で構成されている
 $ Plant    : Ord.factor w/ 12 levels "Qn1"<"Qn2"<"Qn3"<..: 1 1 1 1 1 1 1 2 2 2 ...
 $ Type     : Factor w/ 2 levels "Quebec","Mississippi": 1 1 1 1 1 1 1 1 1 1 ...
 $ Treatment: Factor w/ 2 levels "nonchilled","chilled": 1 1 1 1 1 1 1 1 1 1 ...
 $ conc     : num  95 175 250 350 500 675 1000 95 175 250 ...
 $ uptake   : num  16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ...

CO2データでダミー変数を生成する

# そのままでは順序付き因子型は自動変換されない
str(dummy.data.frame(CO2, sep = "_"))

# ord.factorとfactorを対象に変換
str(dummy.data.frame(CO2, sep = "_", dummy.classes = c("factor","ordered")))  

# カラムを指定してダミー変数化
str(dummy.data.frame(CO2, sep = "_", names = c("Plant", "Type")))

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
What you can do with signing up
7