我有以下格式的数据帧
对于col_1的每个元素,我想从col_2构造一个包含大小为2的所有组合的列。例如,对于大小为2的组合,我的结果将如下所示
我怎么能这么做?
提前感谢!
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包(v2.0.1)于2021-12-13创建
library(tidyverse)
df <- tribble(
~col_1, ~col_2,
"X", "A",
"X", "B",
"X", "C",
"Y", "B",
"Y", "C",
"Z", "A",
"Z", "C",
"Z", "D"
)
df %>%
group_nest(col_1) %>%
transmute(col_1, col_2 = map(data, ~ combn(
x = .x$col_2, m = 2, FUN = toString
))) %>%
unnest(col_2)
#> # A tibble: 7 x 2
#> col_1 col_2
#> <chr> <chr>
#> 1 X A, B
#> 2 X A, C
#> 3 X B, C
#> 4 Y B, C
#> 5 Z A, C
#> 6 Z A, D
#> 7 Z C, D
由reprex包(v2.0.1)于2021-12-13创建
也许这个data. table
选项可以提供帮助
> setDT(df)[, combn(col_2, 2, toString), col_1]
col_1 V1
1: X A, B
2: X A, C
3: X B, C
4: Y B, C
5: Z A, C
6: Z A, D
7: Z C, D