How to one hot encode several categorical variables in R

前端 未结 5 926
再見小時候
再見小時候 2020-12-01 06:55

I\'m working on a prediction problem and I\'m building a decision tree in R, I have several categorical variables and I\'d like to one-hot encode them consistently in my tra

5条回答
  •  Happy的楠姐
    2020-12-01 07:07

    Code

    library(data.table)
    library(mltools)
    customers_1h <- one_hot(as.data.table(customers))
    

    Result

    > customers_1h
    id gender_female gender_male mood_happy mood_sad outcome
    1: 10             0           1          1        0       1
    2: 20             1           0          0        1       1
    3: 30             1           0          1        0       0
    4: 40             0           1          0        1       0
    5: 50             1           0          1        0       0
    

    Data

    customers <- data.frame(
      id=c(10, 20, 30, 40, 50),
      gender=c('male', 'female', 'female', 'male', 'female'),
      mood=c('happy', 'sad', 'happy', 'sad','happy'),
      outcome=c(1, 1, 0, 0, 0))
    

提交回复
热议问题