'Split a string and create dummies in R
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
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | akrun |
