1

I'm trying to do a binary transformation of a comma separated string column in a data frame that looks like this:

df <- data.frame(id = c(1,2,3),
                 mystring = c("test1,test2", "test2,test3", "test1"))

print(df)

  id    mystring
1  1 test1,test2
2  2 test2,test3
3  3       test1

I want to achieve a binary transformation like:

df_result <- data.frame(id = c(1,2,3), 
                        test1 = c(1,0,1), 
                        test2 = c(1,1,0), 
                        test3 = c(0,1,0))

print(df_result)

  id test1 test2 test3
1  1     1     1     0
2  2     0     1     1
3  3     1     0     0
zx8754
  • 52,746
  • 12
  • 114
  • 209
Codutie
  • 1,055
  • 13
  • 25

1 Answers1

1

We can use mtabulate

library(qdapTools)
cbind(df[1], mtabulate(strsplit(as.character(df$mystring), ",")))
akrun
  • 874,273
  • 37
  • 540
  • 662