【问题标题】:expand_grid with identical vectors具有相同向量的 expand_grid
【发布时间】:2021-11-05 18:58:59
【问题描述】:

问题:

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

可重现的例子:

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。仅显示三个向量的唯一组合(任意顺序)。

【问题讨论】:

    标签: r dplyr tidyr


    【解决方案1】:
    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
    

    【讨论】:

      【解决方案2】:

      您可以按行排序并删除重复项。继续你的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
      

      【讨论】:

        【解决方案3】:

        使用 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
        

        【讨论】:

          【解决方案4】:

          基础

          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 创建

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-07-13
            • 1970-01-01
            • 2018-03-10
            • 2016-02-02
            • 2018-06-02
            • 2017-08-09
            • 1970-01-01
            • 2018-05-05
            相关资源
            最近更新 更多