Create several dummy variables from one string variable

前端 未结 4 2014
暗喜
暗喜 2020-12-21 11:30

I\'ve tried pretty much everything from this similar question, but I can\'t get the results everyone else seems to be getting. This is my problem:

I have a data fram

4条回答
  •  温柔的废话
    2020-12-21 12:18

    I've found a workaround. It seems that concat.split.expanded works if you have a string variable containing nothing but separators and numbers, i.e.:

    > profs <- data.frame(teaches = c("1", "1, 2", "2, 3", "1, 2, 3"))
    > profs
      teaches
    1       1
    2    1, 2
    3    2, 3
    4 1, 2, 3
    

    Now concat.split.expanded works as on Dummy variables from a string variable:

    > concat.split.expanded(profs, "teaches", fill = 0, drop = TRUE)
      teaches_1 teaches_2 teaches_3
    1         1         0         0
    2         1         1         0
    3         0         1         1
    4         1         1         1
    

    However, I'm still looking for a solution which doesn't involve removing all letters from my teaches variable.

提交回复
热议问题