我想使用 tidyverse
获得数据帧的唯一字符串列的所有唯一成对组合。 (理想情况下)。
这是一个虚拟示例:
library(tidyverse)
a <- letters[1:3] %>%
tibble::as_tibble()
a
#> # A tibble: 3 x 1
#> value
#> <chr>
#> 1 a
#> 2 b
#> 3 c
tidyr::crossing(a, a) %>%
magrittr::set_colnames(c("words1", "words2"))
#> # A tibble: 9 x 2
#> words1 words2
#> <chr> <chr>
#> 1 a a
#> 2 a b
#> 3 a c
#> 4 b a
#> 5 b b
#> 6 b c
#> 7 c a
#> 8 c b
#> 9 c c
有没有办法在这里删除“重复”组合。在这个例子中,输出如下:
# A tibble: 9 x 2
#> words1 words2
#> <chr> <chr>
#> 1 a b
#> 2 a c
#> 3 b c
我希望会有一个不错的
purrr::map
或 filter
以管入的办法完成上述。编辑:有与此类似的问题,例如here ,由@Sotos 标记。在这里,我专门寻找 tidyverse (
purrr
, dplyr
) 方法来完成我设置的管道。其他答案使用了我不想作为依赖项包含在内的各种其他包。
最佳答案
希望有更好的方法,但我通常使用这个......
library(tidyverse)
df <- tibble(value = letters[1:3])
df %>%
expand(value, value1 = value) %>%
filter(value < value1)
# # A tibble: 3 x 2
# value value1
# <chr> <chr>
# 1 a b
# 2 a c
# 3 b c
关于r - tidyr - 获得组合的独特方式(仅使用 tidyverse),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46491410/