【发布时间】: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 <- 1:63; x <- combn(n, m = 5)? -
确实如此。这就是我要找的。span>
标签: r combinations