Home > front end >  Split a string and create dummies in R
Split a string and create dummies in R

Time:05-11

I have something like val1 where the separator is ,. I would like to split val1 and then create dummies. how to get around with this. many thanks in advance.

val1 <- c("ab, cd, ef", "", "gh")
val2 <- c('John','Peter','Jolie')
data <- data.frame(val1,val2); data
     

Expected Answer

      ab cd ef gh
John  1   1  1  0
Peter 0   0  0  0
Jolie 0   0  0  1

CodePudding user response:

In base R, use strsplit to split the 'val1' based on the , and any space following into a list, set the names of the list with 'val2', stack it to a two column data.frame and apply table

out <- table(stack(setNames(strsplit(data$val1, ",\\s "), data$val2))[2:1])
names(dimnames(out)) <- NULL

-output

> out
      ab cd ef gh
John   1  1  1  0
Peter  0  0  0  0
Jolie  0  0  0  1
  • Related