【问题标题】:find All Combinations of 0's and 1's with specific number of 1's查找 0 和 1 的所有组合以及特定数量的 1
【发布时间】:2018-07-05 22:37:54
【问题描述】:

我想找到一个包含 0 和 1 的所有可能组合的矩阵。这些可能组合的条件是没有个别可能性的声誉,并且对于每个可能的向量都有指定数量的 1。例如,我有多个对象 n = 6,以及多个样本 r = 3,这意味着 6 个插槽,在每个插槽中,(可能的组合),有多个 1 = 3。使用选择() R中的函数,我们可以找到可能性的数量是20。

choose(n=6,k=3) #calculate the number of combinations without replacement/repetition

所有可能组合的理想输出矩阵如下:

1, 1 1 1 0 0 0
2, 1 1 0 1 0 0
3, 1 1 0 0 1 0
4, 1 1 0 0 0 1
5, 1 0 1 1 0 0
6, 1 0 1 0 1 0
7, 1 0 1 0 0 1
8, 0 1 1 1 0 0 
9, 0 1 1 0 1 0
10,0 1 1 0 0 1
11,0 0 1 1 1 0
12,0 0 1 1 0 1
14,0 0 0 1 1 1 
15,1 0 0 1 1 0
16,0 1 0 1 1 0 
17,1 0 0 1 0 1
18,1 0 0 0 1 1 

这些可能性应该等于 20,但是,我发现只有 18 个。 我将把这个概念应用于大量数据集,例如代替 6 个插槽和 3 个 1,它将分别是 200 个插槽和 100 个 1。因此,我需要一个算法或 R 中的内置函数来给我输出。 谢谢。

【问题讨论】:

  • 这些不是组合。它们是排列,因为顺序很重要。另请注意,对于您的真实数据集,您可能必须更改您的方法,因为结果数量巨大:RcppAlgos::permuteCount(0:1, freqs = c(100, 100)) = 9.054851e+58

标签: r combinations


【解决方案1】:
t(combn(6,3,function(x)replace(numeric(6),x,1)))
      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    1    1    0    0    0
 [2,]    1    1    0    1    0    0
 [3,]    1    1    0    0    1    0
 [4,]    1    1    0    0    0    1
 [5,]    1    0    1    1    0    0
 [6,]    1    0    1    0    1    0
 [7,]    1    0    1    0    0    1
 [8,]    1    0    0    1    1    0
 [9,]    1    0    0    1    0    1
[10,]    1    0    0    0    1    1
[11,]    0    1    1    1    0    0
[12,]    0    1    1    0    1    0
[13,]    0    1    1    0    0    1
[14,]    0    1    0    1    1    0
[15,]    0    1    0    1    0    1
[16,]    0    1    0    0    1    1
[17,]    0    0    1    1    1    0
[18,]    0    0    1    1    0    1
[19,]    0    0    1    0    1    1
[20,]    0    0    0    1    1    1

你可以写一个函数:

fun=function(n,m)t(combn(n,m,function(x)replace(numeric(n),x,1)))
fun(6,3

【讨论】:

  • 感谢 Onyambu,太棒了,完美且快速的回答。这似乎是一个非常聪明的答案,所以我需要了解它是如何工作的,我是指函数的每个部分。
  • combn(6,3)开始然后你会看到会发生什么
【解决方案2】:

这只是多集0:1 的简单排列。有几个库能够有效地处理这些问题:RcppAlgos(我是作者)和arrangements

RcppAlgos::permuteGeneral(1:0, freqs = c(3, 3))

arrangements::permutations(x = 1:0, freq = c(3, 3))

两者都给出了预期的结果。您会注意到传递的向量是按降序排列的(即1:0)。之所以如此,是因为这两个库都按字典顺序生成它们的输出。

正如 cmets 中所述,对于您的真实数据,由于结果数量太大,因此发布的解决方案都不起作用。

RcppAlgos::permuteCount(0:1, freqs = c(100,100))
[1] 9.054851e+58

arrangements::npermutations(x = 0:1, freq = c(100, 100), bigz = TRUE)
Big Integer ('bigz') :
[1] 90548514656103281165404177077484163874504589675413336841320

由于一次生成如此大量的数据根本不可行,arrangementsRcppAlgos 这两个软件包都提供了替代方法,可以让人们解决更大的问题。

安排

对于包arrangements,您可以设置一个迭代器,允许用户一次生成组合/排列n,避免生成所有的开销他们。

library(arrangements)
iperm <- ipermutations(x = 1:0, freq = c(3,3))

## get the first 5 permutations
iperm$getnext(d = 5)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    0    0    0
[2,]    1    1    0    1    0    0
[3,]    1    1    0    0    1    0
[4,]    1    1    0    0    0    1
[5,]    1    0    1    1    0    0

## get the next 5 permutations
iperm$getnext(d = 5)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    1    0    1    0
[2,]    1    0    1    0    0    1
[3,]    1    0    0    1    1    0
[4,]    1    0    0    1    0    1
[5,]    1    0    0    0    1    1


RcppAlgos

对于RcppAlgos,有参数lowerupper 允许生成特定块。

library(RcppAlgos)
permuteGeneral(1:0, freqs = c(3,3), lower = 1, upper = 5)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    1    1    0    0    0
[2,]    1    1    0    1    0    0
[3,]    1    1    0    0    1    0
[4,]    1    1    0    0    0    1
[5,]    1    0    1    1    0    0

permuteGeneral(1:0, freqs = c(3,3), lower = 6, upper = 10)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    0    1    0    1    0
[2,]    1    0    1    0    0    1
[3,]    1    0    0    1    1    0
[4,]    1    0    0    1    0    1
[5,]    1    0    0    0    1    1

由于这些块是独立生成的,因此可以轻松地并行生成和分析:

library(parallel)
mclapply(seq(1,20,5), function(x) {
    a <- permuteGeneral(1:0, freqs = c(3,3), lower = x, upper = x + 4)
    ## Do some analysis
}, mc.cores = detectCores() - 1)

对于这个小示例,您不会注意到任何加速,但随着结果数量的增加,会有明显的提升。

summary 中有更多关于这个主题的信息,我写信给这个问题:R: Permutations and combinations with/without replacement and for distinct/non-distinct items/multiset

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    相关资源
    最近更新 更多