rpermutation

Obtaining random sample from all permutations


I have a vector of length 100 and I want to get a sample of certain size like 50, from all permutations of length 20 from elements of my original vector

My attempt so far is to first to get all permutations then select a sample of size 50

First part

All_Permutation = gtools::permutations(n = 100, r = 20, v = 1:100)

Second part

I could not complete because, the first part is taking considerable amount of time and memory

I wonder is there any more manageable way to get that sample?


Solution

  • Try RcppAlgos::permuteSample.

    > RcppAlgos::permuteSample(v=1:100, m=20, n=50, seed=42)
          [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20]
     [1,]   13   87   36  100   62   58   30   64   49    72    57     2    28    17    41    88    40    85    65    76
     [2,]   45   12   27   18   69   57   52   26    3    29    63    17    11    82    70    50    75    78    38     6
     [3,]   49   11   81   92   15   65   78   67   93    62    46    33    25    73    83    10    57    38    41    56
     [4,]   60   97   72    4   35   19   22   66   37   100    61    81    34    14    40    89    11    13     3    74
     [5,]    7   41   51   56   59   18   32   39   99    97    15    95    83    85     4    84    33    10    91    40
     [6,]   86   43   82   12   33   36   41   96   81    63    26    34    17     4    69    10    53    59    76    71
    ...
    

    You can also calculate the number of permutations:

    > RcppAlgos::permuteCount(v=1:100, m=20)
    Big Integer ('bigz') :
    [1] 1303995018204712451095685346159820800000