我想使用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
Run Code Online (Sandbox Code Playgroud)
有没有办法在这里删除“重复”的组合。在此示例中,输出如下:
# A tibble: 9 x 2
#> words1 words2
#> <chr> <chr>
#> 1 a b
#> 2 a c
#> 3 b c
Run Code Online (Sandbox Code Playgroud)
我希望会有一个好的purrr::map或filter办法管到完成以上。
编辑:有与此类似的问题,例如在这里,用@Sotos标记。在这里,我专门寻找tidyverse(purrr,dplyr)方式来完成已设置的管道。其他答案使用了其他各种我不想包含为依赖的软件包。
希望有更好的方法,但我通常使用此方法...
library(tidyverse)
df <- tibble(value = letters[1:3])
df %>%
expand(value, value) %>%
filter(value < value1)
# # A tibble: 3 x 2
# value value1
# <chr> <chr>
# 1 a b
# 2 a c
# 3 b c
Run Code Online (Sandbox Code Playgroud)