Splitting string column into many using tidyverse

  • A+

I have a string column with commas. I want to convert this single column into multiple labeled columns, with values filled in appropriately. The output data frame would have 3 columns (A, B, and C). Row 1 would have columns A and B filled in with "Yes", and C with "No". Row 2 would have all 3 columns filled with "Yes", etc.

df1 <- data.frame(X= c("A, B", "A, B, C", "A", "A, C"))  df1         X 1    A, B 2 A, B, C 3       A 4    A, C 

Required Output

A    B    C Yes  Yes  No Yes  Yes  Yes Yes  No   No Yes  No   Yes 

Any hint, please.


Something like this:

library(tidyverse)  df1 %>%   mutate(id = row_number()) %>%    separate_rows(X) %>%    group_by(id) %>%    mutate(Y = "yes") %>%    spread(X, Y, fill = "no")  # A tibble: 4 x 4 # Groups:   id [4]      id A     B     C       <int> <chr> <chr> <chr> 1     1 yes   yes   no    2     2 yes   yes   yes   3     3 yes   no    no    4     4 yes   no    yes   


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: