Home > OS >  How to Convert Factor to Dummy Variables?
How to Convert Factor to Dummy Variables?

Time:12-03

> dput(head(final,10))
structure(list(Y = c(93.433, 104.456, 163.792, 125.249, 146.837, 
78.196, 52.192, 191.33, 75.02, 145.785), X1 = c(5.9701, 9.3506, 
9.718, 14.1317, 9.9278, 1.9318, 2.2236, 12.612, 13.8961, 8.1844
), X2 = c(6.047, 9.4063, 9.4967, 13.9422, 10.0581, 1.6575, 1.8749, 
12.3052, 13.7316, 8.2732), X3 = c(8.1105, 8.365, 16.8862, 14.8049, 
14.1477, 15.9753, 12.0362, 16.5604, 8.1691, 16.4479), x4 = c(1.70843, 
0.34726, 4.76446, 2.19965, 2.80567, 7.58081, 5.59927, 3.56611, 
-1.10324, 4.76204), x5 = c(1, 1, 1, 2, 1, 1, 3, 1, 2, 1)), row.names = c(NA, 
10L), class = "data.frame")

x5 is my factor variable, which has type 1, 2, 3. Now I want to create x6 and x7 such that:

Types x6 x7

type1 0 0

type2 1 0

type3 0 1

How to do?

CodePudding user response:

You can use model.matrix:

# Packages
library(magrittr)
library(dplyr)
library(stringr)
# Make x5 a factor
final <- final %>% 
    as_tibble() %>% 
    mutate(
        x5 = as.factor(x5)
    )
# Make the dummy variables
final <- model.matrix(~0 final$x5) %>% 
     as_tibble() %>% 
     rename_all(~str_remove_all(., '.*\\$')) %>% 
     mutate(
         across(everything(), as.factor)
     ) %>% 
     bind_cols(final, .) %>% 
     select(-x5)
# A tibble: 10 x 8
       Y    X1    X2    X3     x4 x51   x52   x53  
   <dbl> <dbl> <dbl> <dbl>  <dbl> <fct> <fct> <fct>
 1  93.4  5.97  6.05  8.11  1.71  1     0     0    
 2 104.   9.35  9.41  8.36  0.347 1     0     0    
 3 164.   9.72  9.50 16.9   4.76  1     0     0    
 4 125.  14.1  13.9  14.8   2.20  0     1     0    
 5 147.   9.93 10.1  14.1   2.81  1     0     0    
 6  78.2  1.93  1.66 16.0   7.58  1     0     0    
 7  52.2  2.22  1.87 12.0   5.60  0     0     1    
 8 191.  12.6  12.3  16.6   3.57  1     0     0    
 9  75.0 13.9  13.7   8.17 -1.10  0     1     0    
10 146.   8.18  8.27 16.4   4.76  1     0     0 
  •  Tags:  
  • r
  • Related