Split a string column into several dummy variables

前端 未结 6 684
醉梦人生
醉梦人生 2020-12-01 22:13

As a relatively inexperienced user of the data.table package in R, I\'ve been trying to process one text column into a large number of indicator columns (dummy variables), w

6条回答
  •  醉酒成梦
    2020-12-01 22:18

    Here's a somewhat newer approach, using cSplit_e() from the splitstackshape package.

    library(splitstackshape)
    cSplit_e(dt, split.col = "String", sep = "$", type = "character", 
             mode = "binary", fixed = TRUE, fill = 0)
    #  ID String String_a String_b String_c
    #1  1    a$b        1        1        0
    #2  2    b$c        0        1        1
    #3  3      c        0        0        1
    

提交回复
热议问题