Search code examples
rsampling

Enumerate all combinations of size N from k items with N > k


I have a problem where I have k items, say {0,1}, and I have to enumerate all possible N draws, say N=3. That is, I am trying to find all possible samples of a given size from a vector, with replacement.

I can get there via the following loop approach:

for (i1 in c(0,1)){
    for (i2 in  c(0,1)){
        for (i3 in c(0,1)){
             print(paste(i1,i2i3,collapse="_")) 
}}} 

However, this feels like a kludge. Is there a better way to do this using base R?


Solution

  • If vec is your vector and n is the number of times you draw from vec, to enumerate all the possibilities, try:

      expand.grid(replicate(n,vec,simplify=FALSE),
           KEEP.OUT.ATTRS=FALSE,stringsAsFactors=FALSE)
    

    For instance:

     vec<-0:1
     n<-3
     expand.grid(replicate(n,vec,simplify=FALSE),
           KEEP.OUT.ATTRS=FALSE,stringsAsFactors=FALSE)
     #   Var1 Var2 Var3
     #1    0    0    0
     #2    1    0    0
     #3    0    1    0
     #4    1    1    0
     #5    0    0    1
     #6    1    0    1
     #7    0    1    1
     #8    1    1    1  
    

    The number of permutations rises very quickly; try the above only for small vectors and small n.