【问题标题】:find all unique combinations of n numbers between 1 and k找出 1 到 k 之间的 n 个数字的所有唯一组合
【发布时间】:2018-07-06 08:02:13
【问题描述】:

我想要一个 1 到 63(或更概括地说是 1 到 k)之间的所有可能的五个(或 n)个数字集的列表

如果计算时间不是问题,我可以做类似的事情

 #Get all combenations of numbers between 1 and 63
 indexCombinations <- expand.grid(1:63, 1:63, 1:63, 1:63, 1:63)

 #Throw out the rows that have more than one of the same number in them
 allDifferent <- apply(indexCombinations, 1, function(x){
      length(x) == length(unique(x))
 } # function
 ) # apply

 indexCombinationsValid <- indexCombinations[allDifferent,]

 # And then just take the unique values
 indexCombinationsValidUnique <- unique(indexCombinationsValid)

我担心,唯一值的发现将会非常缓慢。此外,我最终不得不首先制作一堆我从未使用过的行。我想知道是否有人有一种更优雅、更有效的方法来获取一个数据框或矩阵,该矩阵由五个数字(或 n 个数字)中的每一个的唯一组合组成,该组合介于一个值和某个值范围之间。

【问题讨论】:

  • 这是你正在尝试的:n &lt;- 1:63; x &lt;- combn(n, m = 5) ?
  • 确实如此。这就是我要找的。​​span>

标签: r combinations


【解决方案1】:

感谢@SymbolixAU 提供了一个非常优雅的解决方案,我在这里重新发布作为答案:

 n <- 1:63; x <- combn(n, m = 5)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    相关资源
    最近更新 更多