R:长度为 n 的二进制向量的所有可能组合,其中 m 的每个组合中只有一个值为“1”

R: all possible combinations of a binary vector of length n, where only one value is “1” in each combination of m

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)

这与您对那个问题的另一个 . In 非常相似,我们看到重新措辞这个问题会使攻击变得容易得多。那么对于这道题,我们可以化简为:"How to generate all pairwise permutations of powers of 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

 .      .    .    .    .
 .      .    .    .    .
 .      .    .    .    .