2017-08-17 31 views
1

我想要的1到63之間的所有可能的套五(或N)數量列表(或更多generalizably 1和k)找到1之間的n個數的所有獨特combenations和k

如果計算時間WASN」一個問題,我可以做類似

#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個數字)的每一個的獨特組合。

+2

這是你想要什麼:'ñ< - 1:63; x < - combn(n,m = 5)'? – SymbolixAU

+0

是的。這是我正在尋找的。 – ohnoplus

回答

2

感謝@SymbolixAU一個非常優雅的解決方案,這是我重新張貼在這裏作爲一個答案:

n <- 1:63; x <- combn(n, m = 5) 
相關問題