Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading