我想要一个包含 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 个数字)中每一个的唯一组合矩阵。
感谢@SymbolixAU 提供了一个非常优雅的解决方案,我在这里重新发布作为答案:
n <- 1:63; x <- combn(n, m = 5)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句