【问题标题】:R: all possible combinations of a binary vector of length n, where only one value is “1” in each combination of mR:长度为 n 的二进制向量的所有可能组合,其中 m 的每个组合中只有一个值是“1”
【发布时间】:2018-01-18 05:31:20
【问题描述】:

在 R 中有没有办法创建 m 的二进制集合,按列填充 n 的所有组合,而按行只有 1 个值可以是“1”?

例如,对于 n=2 和 m=2,我们将有以下 m 的组合:

(00, 00), (10,00), (01,00), (00,10), (00,01), (10,01), (01,10), (10,10), (01,01)

但这些,例如,不允许: (11,00), (01,11), (00,11), (11,10), (11,11)

【问题讨论】:

    标签: r combinations combinatorics


    【解决方案1】:

    这与您的其他question 非常相似。在该问题的my answer 中,我们看到改写问题会使攻击变得更容易。所以对于这个问题,我们可以将其简化为:“如何生成所有 2 的幂的成对排列with repeats?”

    我们可以使用几乎与之前完全相同的设置,只是这次我们设置了参数repeats.allowed = TRUE

    library(gtools)
    bitPairwise2 <- function(numBits, groupSize) {
      t(sapply(t(permutations(numBits + 1, groupSize, 
                               c(0, 2^(0:(numBits-1))), repeats.allowed = TRUE)), 
               function(x) {as.integer(intToBits(x))})[1:numBits, ])
    }
    
    bitPairwise2(2,2)
          [,1] [,2]
     [1,]    0    0    ## (00,00)
     [2,]    0    0
    
     [3,]    0    0    ## (00,10)
     [4,]    1    0
    
     [5,]    0    0    ## (00,01)
     [6,]    0    1
    
     [7,]    1    0    ## (10,00)
     [8,]    0    0
    
     [9,]    1    0    ## (10,10)
    [10,]    1    0
    
    [11,]    1    0    ## (10,01)
    [12,]    0    1
    

    此函数可推广到任意数量的位以及任意数量的组。例如,所有可能的 3 位 3 元组由下式给出:

    ## first 9 groups
    bitPairwise2(3, 3)[1:27, ]
          [,1] [,2] [,3]
     [1,]    0    0    0    ## (000,000,000)
     [2,]    0    0    0
     [3,]    0    0    0
    
     [4,]    0    0    0    ## (000,000,100)
     [5,]    0    0    0
     [6,]    1    0    0
    
     [7,]    0    0    0    ## (000,000,010)
     [8,]    0    0    0
     [9,]    0    1    0
    
    [10,]    0    0    0    ## (000,000,001)
    [11,]    0    0    0
    [12,]    0    0    1
    
    [13,]    0    0    0    ## (000,100,000)
    [14,]    1    0    0
    [15,]    0    0    0
    
    [16,]    0    0    0    ## (000,100,100)
    [17,]    1    0    0
    [18,]    1    0    0
    
    [19,]    0    0    0    ## (000,100,010)
    [20,]    1    0    0
    [21,]    0    1    0
    
    [22,]    0    0    0    ## (000,100,001)
    [23,]    1    0    0
    [24,]    0    0    1
    
    [25,]    0    0    0    ## (000,010,000)
    [26,]    0    1    0
    [27,]    0    0    0
    

    这是最后 9 组:

    a <- bitPairwise2(3, 3)[166:192, ]
    row.names(a) <- 166:192
    a
        [,1] [,2] [,3]
    166    0    0    1    ## (001,100,001)
    167    1    0    0
    168    0    0    1
    
    169    0    0    1    ## (001,010,000)
    170    0    1    0
    171    0    0    0
    
    172    0    0    1    ## (001,010,100)
    173    0    1    0
    174    1    0    0
    
    175    0    0    1    ## (001,010,010)
    176    0    1    0
    177    0    1    0
    
    178    0    0    1    ## (001,010,001)
    179    0    1    0
    180    0    0    1
    
    181    0    0    1    ## (001,001,000)
    182    0    0    1
    183    0    0    0
    
    184    0    0    1    ## (001,001,100)
    185    0    0    1
    186    1    0    0
    
    187    0    0    1    ## (001,001,010)
    188    0    0    1
    189    0    1    0
    
    190    0    0    1    ## (001,001,001)
    191    0    0    1
    192    0    0    1
    

    如果您需要列表中的输出,请尝试以下操作:

    test <- bitPairwise2(4, 3)
    numGroups <- nrow(test)/3
    
    makeGroupList <- function(mat, nG, groupSize) {
        lapply(1:nG, function(x) {
            s <- groupSize*(x-1) + 1
            e <- s + (groupSize - 1)
            mat[s:e, ]
        })
    }
    
    makeGroupList(test, numGroups, 3)
    [[1]]
         [,1] [,2] [,3] [,4]
    [1,]    0    0    0    0
    [2,]    0    0    0    0
    [3,]    0    0    0    0
    
    [[2]]
         [,1] [,2] [,3] [,4]
    [1,]    0    0    0    0
    [2,]    0    0    0    0
    [3,]    1    0    0    0
    
    [[3]]
         [,1] [,2] [,3] [,4]
    [1,]    0    0    0    0
    [2,]    0    0    0    0
    [3,]    0    1    0    0
    
     .      .    .    .    .
     .      .    .    .    .
     .      .    .    .    .
    

    【讨论】:

    • 我很敬畏。非常感谢 Joseph,感谢您的精彩解释。
    • 最后一个问题,我在使用时遇到错误,例如bitPairwise2(109, 3) - sapply(t(permutations(numBits + 1, groupSize, c(0, 2^(0:(numBits - subscript out of bounds, 是因为 R 限制吗?组合这么棒吗?再次感谢。
    • @user971102 收到错误消息后,您还应该收到消息In addition: There were 50 or more warnings (use warnings() to see the first 50)。如果您输入warnings(),您将看到In intToBits(x) : NAs introduced by coercion to integer range。这是因为向量c(0, 2^(0:(numBits - 1))numBits = 109 的最大元素为2^108 = 3.245186e+32。对于intToBit 来说,这是一种很大的工作方式(它只接受整数(max = 2^31 - 1))。您将需要一种不同的方法来处理这种规模的问题。
    • 知道了!谢谢!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-01
    • 2016-04-23
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2018-06-21
    • 1970-01-01
    相关资源
    最近更新 更多