Home > Net >  how to change categories into dummy variables from several columns
how to change categories into dummy variables from several columns

Time:11-11

I have a variable with a lot of categories, individuals were asked to choose their first 3 choices. So the choices are A, B, C and D here (It's true that in my database there are more than 4 possible choices)

`var1:choice1`<-c("A","B","C")

`var1:choice2`<-c("B","C","A")

`var1:choice3`<-c("C","D","B")

id=1:3

mydata<-data.frame(id,`var1:choice1`,`var1:choice2`,`var1:choice3`)

Now I want to change the choices into an dummy variables, so that I can have the number of occurrences of each choice. I should have this table below:

 var1.A var1.B var1.C var1.D
1      1      1      1      0
2      0      1      1      1
3      1      1      1      0

CodePudding user response:

In base R you could create a simple table:

table(rep(mydata[[1]], 3), paste0('var1.', unlist(mydata[-1])))
   
    var1.A var1.B var1.C var1.D
  1      1      1      1      0
  2      0      1      1      1
  3      1      1      1      0

CodePudding user response:

Here's an approach:

mydata %>% 
  pivot_longer(-id) %>% 
  mutate(var = paste0(substr(name, 1, 5), value)) %>% 
  select(id, var, value) %>% 
  pivot_wider(names_from = var, values_from = value, values_fn = length, values_fill = 0)

# A tibble: 3 × 5
     id var1.A var1.B var1.C var1.D
  <int>  <int>  <int>  <int>  <int>
1     1      1      1      1      0
2     2      0      1      1      1
3     3      1      1      1      0

CodePudding user response:

With pivot_wider and pivot_longer:

library(tidyr)
library(dplyr)
mydata %>% 
  pivot_longer(-id,
               names_to = c("var", "choice"),
               names_pattern = "var(\\d).choice(\\d)") %>% 
  pivot_wider(names_from = value, names_prefix = "var1.",
              values_from = choice, values_fn = length, values_fill = 0) %>% 
  select(-c(id, var))

# A tibble: 3 × 4
  var1.A var1.B var1.C var1.D
   <int>  <int>  <int>  <int>
1      1      1      1      0
2      0      1      1      1
3      1      1      1      0
  •  Tags:  
  • r
  • Related