expand_grid 具有相同的向量

expand_grid with identical vectors

问题:

有没有一种简单的方法可以得到两个(或更多)相同向量的所有组合。但只显示独特的组合。

可重现的例子:

library(tidyr)

x = 1:3

expand_grid(a = x, 
            b = x, 
            c = x)

# A tibble: 27 x 3
       a     b     c
   <int> <int> <int>
 1     1     1     1
 2     1     1     2
 3     1     1     3
 4     1     2     1
 5     1     2     2
 6     1     2     3
 7     1     3     1
 8     1     3     2
 9     1     3     3
10     2     1     1
# ... with 17 more rows

但是,如果行 1 2 1 存在,那么我不想看到 1 1 22 1 1。 IE。仅显示三个向量的唯一组合(任意顺序)。

library(gtools)
x = 1:3
df <- as.data.frame(combinations(n=3,r=3,v=x,repeats.allowed=T))
df

输出

  V1 V2 V3
1   1  1  1
2   1  1  2
3   1  1  3
4   1  2  2
5   1  2  3
6   1  3  3
7   2  2  2
8   2  2  3
9   2  3  3
10  3  3  3

您可以按行排序并删除重复项。从您的 expand_grid() 继续,然后

df <- tidyr::expand_grid(a = x, 
            b = x, 
            c = x)

data.frame(unique(t(apply(df, 1, sort))))


   X1 X2 X3
1   1  1  1
2   1  1  2
3   1  1  3
4   1  2  2
5   1  2  3
6   1  3  3
7   2  2  2
8   2  2  3
9   2  3  3
10  3  3  3

基础

x <- 1:3

df <- expand.grid(a = x, 
            b = x, 
            c = x)

df[!duplicated(apply(df, 1, function(x) paste(sort(x), collapse = ""))), ]
#>    a b c
#> 1  1 1 1
#> 2  2 1 1
#> 3  3 1 1
#> 5  2 2 1
#> 6  3 2 1
#> 9  3 3 1
#> 14 2 2 2
#> 15 3 2 2
#> 18 3 3 2
#> 27 3 3 3

reprex package (v2.0.1)

于 2021-09-09 创建

使用 RcppAlgos 包中的 comboGeneral,它是用 C++ 实现的,速度非常快。

x <- 1:3
RcppAlgos::comboGeneral(x, repetition=TRUE)
#       [,1] [,2] [,3]
#  [1,]    1    1    1
#  [2,]    1    1    2
#  [3,]    1    1    3
#  [4,]    1    2    2
#  [5,]    1    2    3
#  [6,]    1    3    3
#  [7,]    2    2    2
#  [8,]    2    2    3
#  [9,]    2    3    3
# [10,]    3    3    3

注意: 如果您是 运行 Linux,则需要安装 gmp,例如对于 Ubuntu 做:

sudo apt install libgmp3-dev