-2

I want to represent factor variables as 0 and 1 value through one hot encoding in r as data.frame.

Among the factor variables, I would like to perform one hot encode only for variables with three or more levels.

This is my R code.

german<-read.csv("http://freakonometrics.free.fr/german_credit.csv", header=TRUE)
F=c(1,2,4,5,7,8,9,10,11,12,13,15,16,17,18,19,20,21)
for(i in F) german[,i]=as.factor(german[,i])
str(german)
'data.frame':   1000 obs. of  21 variables:
 $ Creditability                    : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...
 $ Account.Balance                  : Factor w/ 4 levels "1","2","3","4": 1 1 2 1 1 1 1 1 4 2 ...
 $ Duration.of.Credit..month.       : int  18 9 12 12 12 10 8 6 18 24 ...
 $ Payment.Status.of.Previous.Credit: Factor w/ 5 levels "0","1","2","3",..: 5 5 3 5 5 5 5 5 5 3 ...
 $ Purpose                          : Factor w/ 10 levels "0","1","2","3",..: 3 1 9 1 1 1 1 1 4 4 ...
 $ Credit.Amount                    : int  1049 2799 841 2122 2171 2241 3398 1361 1098 3758 ...
 $ Value.Savings.Stocks             : Factor w/ 5 levels "1","2","3","4",..: 1 1 2 1 1 1 1 1 1 3 ...
 $ Length.of.current.employment     : Factor w/ 5 levels "1","2","3","4",..: 2 3 4 3 3 2 4 2 1 1 ...
 $ Instalment.per.cent              : Factor w/ 4 levels "1","2","3","4": 4 2 2 3 4 1 1 2 4 1 ...
 $ Sex...Marital.Status             : Factor w/ 4 levels "1","2","3","4": 2 3 2 3 3 3 3 3 2 2 ...
 $ Guarantors                       : Factor w/ 3 levels "1","2","3": 1 1 1 1 1 1 1 1 1 1 ...
 $ Duration.in.Current.address      : Factor w/ 4 levels "1","2","3","4": 4 2 4 2 4 3 4 4 4 4 ...
 $ Most.valuable.available.asset    : Factor w/ 4 levels "1","2","3","4": 2 1 1 1 2 1 1 1 3 4 ...
 $ Age..years.                      : int  21 36 23 39 38 48 39 40 65 23 ...
 $ Concurrent.Credits               : Factor w/ 3 levels "1","2","3": 3 3 3 3 1 3 3 3 3 3 ...
 $ Type.of.apartment                : Factor w/ 3 levels "1","2","3": 1 1 1 1 2 1 2 2 2 1 ...
 $ No.of.Credits.at.this.Bank       : Factor w/ 4 levels "1","2","3","4": 1 2 1 2 2 2 2 1 2 1 ...
 $ Occupation                       : Factor w/ 4 levels "1","2","3","4": 3 3 2 2 2 2 2 2 1 1 ...
 $ No.of.dependents                 : Factor w/ 2 levels "1","2": 1 2 1 2 1 2 1 2 1 1 ...
 $ Telephone                        : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
 $ Foreign.Worker                   : Factor w/ 2 levels "1","2": 1 1 1 2 2 2 2 2 1 1 ...

In here, I want to one hot encode the factor variables that have more than 3 levels.

For example, Guarantors variable has 3 levels 1,2,3. As a result, I want to get Guarantors1, Guarantors2 and Guarantors3 variables that have only 0,1 value as data.frame.

lmo
  • 35,764
  • 9
  • 49
  • 57
신익수
  • 67
  • 3
  • 7
  • 1
    Show us what you've tried on your own. We're not a code writing service. – emilliman5 Aug 27 '17 at 16:45
  • Possible duplicate of [Create new dummy variable columns from categorical variable](https://stackoverflow.com/questions/3384506/create-new-dummy-variable-columns-from-categorical-variable) – Aramis7d Aug 27 '17 at 22:48

1 Answers1

2

a dplyr & purrr approach

library(dplyr)
library(purrr)

german<-read.csv("http://freakonometrics.free.fr/german_credit.csv", header=TRUE)

cols <- c(1,2,4,5,7,8,9,10,11,12,13,15,16,17,18,19,20,21)

map_df(german[, cols], as.factor) %>% 
      select_if(function(x) nlevels(x) >= 2) %>% 
      model.matrix(~. -1, data = .) %>% 
      as.data.frame()

I would recommend reading the help model.matrix, or other questions from SO on this topic.

Jake Kaupp
  • 7,097
  • 2
  • 21
  • 34