[英]find all unique combinations of n numbers between 1 and k
I want a list of all possible sets of five (or n) numbers between 1 and 63 (or more generalizably 1 and k) 我想要一个在1到63之间(或更笼统地说是1到k)的五个(或n个)数字的所有可能集合的列表
If computing time wasn't an issue, I could do something like 如果计算时间不是问题,我可以做类似的事情
#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)
The finding of unique values, I am concerned, is going to be prohibitively slow. 我担心,发现独特价值的步伐将非常缓慢。 Furthermore, I end up having to make a bunch of rows in the first place I never use.
此外,我最终不得不一排排从未使用过的行。 I was wondering if anyone has a more elegant and efficient way of getting a data frame or matrix of unique combinations of each of five numbers (or n numbers) between one and some some range of values.
我想知道是否有人有一种更优雅,更有效的方法来获取一个值和某个值范围之间的五个数字(或n个数字)的唯一组合的数据框或矩阵。
感谢@SymbolixAU提供一个非常优雅的解决方案,我将其在此处重新发布作为答案:
n <- 1:63; x <- combn(n, m = 5)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.