Search code examples
rreshapereshape2strsplittidyr

Splitting one Column to Multiple R and Giving logical value if true


I am trying to split one column in a data frame in to multiple columns which hold the values from the original column as new column names. Then if there was an occurrence for that respective column in the original give it a 1 in the new column or 0 if no match. I realize this is not the best way to explain so, for example:

df <- data.frame(subject = c(1:4), Location = c('A', 'A/B', 'B/C/D', 'A/B/C/D'))  

#   subject Location  
# 1       1     A                                  
# 2       2     A/B                                   
# 3       3     B/C/D                                 
# 4       4     A/B/C/D

and would like to expand it to wide format, something such as, with 1's and 0's (or T and F):

#   subject    A  B  C  D
# 1       1    1  0  0  0
# 2       2    1  1  0  0
# 3       3    0  1  1  1
# 4       4    1  1  1  1  

I have looked into tidyr and the separate function and reshape2 and the cast function but seem to getting hung up on giving logical values. Any help on the issue would be greatly appreciated. Thank you.


Solution

  • You may try cSplit_e from package splitstackshape:

    library(splitstackshape)
    cSplit_e(data = df, split.col = "Location", sep = "/",
             type = "character", drop = TRUE, fill = 0)
    #   subject Location_A Location_B Location_C Location_D
    # 1       1          1          0          0          0
    # 2       2          1          1          0          0
    # 3       3          0          1          1          1
    # 4       4          1          1          1          1