library(tidyverse)
data <- tribble(
~col_1, ~col_2,
"X", "A",
"X", "B",
"X", "C",
"Y", "B",
"Y", "C",
"Z", "A",
"Z", "C",
"Z", "D"
)
data2 <-
data %>%
nest(-col_1) %>%
mutate(
data = data %>% map(~ {
.x$col_2 %>%
combn(2) %>%
t() %>%
as_tibble() %>%
transmute(col_2 = map2(V1, V2, ~ c(.x, .y)))
})
) %>%
unnest(data)
#> Warning: All elements of `...` must be named.
#> Did you want `data = c(col_2)`?
#> Warning: The `x` argument of `as_tibble.matrix()` must have unique column names if `.name_repair` is omitted as of tibble 2.0.0.
#> Using compatibility `.name_repair`.
# reduplicate rows
data %>%
select(col_1) %>%
left_join(data2) %>%
as.data.frame()
#> Joining, by = "col_1"
#> col_1 col_2
#> 1 X A, B
#> 2 X A, C
#> 3 X B, C
#> 4 X A, B
#> 5 X A, C
#> 6 X B, C
#> 7 X A, B
#> 8 X A, C
#> 9 X B, C
#> 10 Y B, C
#> 11 Y B, C
#> 12 Z A, C
#> 13 Z A, D
#> 14 Z C, D
#> 15 Z A, C
#> 16 Z A, D
#> 17 Z C, D
#> 18 Z A, C
#> 19 Z A, D
#> 20 Z C, D
由reprex package (v2.0.1) 于 2021 年 12 月 13 日创建