Vinnish
可以试试 named list-column + tidyr::unnest_wider
library(dplyr, warn.conflicts = F)
library(tidyr)
library(purrr)
library(stringr)
df <- tibble(x = c('a, b', 'c', 'a, c, d'))
df1 <- df %>%
mutate(
y = str_split(x, ', '),
y = map(y, ~ .x %>% set_names(.x))
) %>%
unnest_wider(y)
df1
#> # A tibble: 3 × 5
#> x a b c d
#> <chr> <chr> <chr> <chr> <chr>
#> 1 a, b a b <NA> <NA>
#> 2 c <NA> <NA> c <NA>
#> 3 a, c, d a <NA> c d
df1 %>%
mutate(across(!x, ~ if_else(is.na(.x), 0, 1)))
#> # A tibble: 3 × 5
#> x a b c d
#> <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 a, b 1 1 0 0
#> 2 c 0 0 1 0
#> 3 a, c, d 1 0 1 1
这里楼上的长宽表转化就够了,不需要这么花里胡哨的方法 😆