R 查找1和k之间n个数字的所有唯一组合
我想要一个由5个(或n个)数字组成的列表,这些数字在1和63之间(或者更概括地说是1和k) 如果计算时间不是问题的话,我可以这样做R 查找1和k之间n个数字的所有唯一组合,r,combinations,R,Combinations,我想要一个由5个(或n个)数字组成的列表,这些数字在1和63之间(或者更概括地说是1和k) 如果计算时间不是问题的话,我可以这样做 #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 i
#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)
#获取1到63之间的所有数字组合
indexCombinations感谢@SymbolXau提供了一个非常优雅的解决方案,我在这里重新发布了这个解决方案,作为一个答案:
n <- 1:63; x <- combn(n, m = 5)
这就是你要尝试的:n是的。这就是我要找的。