rcombinationscombn

R find all possible unique combinations


I am trying to find all possible unique combinations in R. It seems that there have been a lot of similar questions asked, but I was not able to find the same one.

My question is to find combinations of m number of elements from vector x, but m could be larger than x. For example, pick 3 elements from letters[1:2], which hopefully can return:

combn(letters[1:2],3)
       [,1]  [,2]  [,3]  [,4] 
[1,]   "a"   "a"   "a"    "b"
[2,]   "a"   "a"   "b"    "b"
[3,]   "a"   "b"   "b"    "b"

But instead error in combn function n < m. There are similar functions including gtools:permutations, expand.grid.

Apologize again if someone asked the same questions previously, but I did not catch it. Thanks.


Solution

  • There are a few packages specifically built for this. The basic premise is that we need combinations with repetition of length m where m could be larger than the input vector. We start with the classic gtools:

    library(gtools)
    combinations(2, 3, letters[1:2], repeats.allowed = TRUE)
    #>      [,1] [,2] [,3]
    #> [1,] "a"  "a"  "a" 
    #> [2,] "a"  "a"  "b" 
    #> [3,] "a"  "b"  "b" 
    #> [4,] "b"  "b"  "b"
    

    And then there is arrangements which is a replacement for iterpc (the package linked by @Gregor in the comments above):

    ## library(arrangements)
    arrangements::combinations(letters[1:2], 3, replace = TRUE)
    #>      [,1] [,2] [,3]
    #> [1,] "a"  "a"  "a" 
    #> [2,] "a"  "a"  "b" 
    #> [3,] "a"  "b"  "b" 
    #> [4,] "b"  "b"  "b"
    

    And finally there is RcppAlgos, which I authored:

    library(RcppAlgos)
    comboGeneral(letters[1:2], 3, TRUE)
    #>      [,1] [,2] [,3]
    #> [1,] "a"  "a"  "a" 
    #> [2,] "a"  "a"  "b" 
    #> [3,] "a"  "b"  "b" 
    #> [4,] "b"  "b"  "b"
    

    combn is an awesome function that ships as one of the base packages with R, however one of its shortcomings is that it doesn’t allow repetition (which is what is required here). I wrote a comprehensive overview for problems exactly like this one that can be found here: A Walk Through a Slice of Combinatorics in R.