Search code examples
rcombinationspermutationlarge-data

r - combinations from elements in vector


Given the vector:

a <- c(1,2,3)

I'm trying to calculate all vectors containing combinations from the elements in a, which is:

list(
    a[c(1,2,3)],
    a[c(1,3,2)],
    a[c(2,1,3)],
    a[c(2,3,1)],
    a[c(3,1,2)],
    a[c(3,2,1)])

This can be reproduced with:

df <- expand.grid(rep(list(a), length(a)))
nunique <- apply(df, 1, function(x) length(unique(x)))
df <- df[nunique == ncol(df), ]
as.list(as.data.frame(t(df)))

I tried doing this with the expand.grid but this function gives permutations where elements can be repeated which results in extra large dataset and gives the error from below.

I have seen similar questions to this but fail to find a fast solution that doesn't produce error:

Error: cannot allocate vector of size 37.3 Gb

The error can be reproduced for:

a <- c(1,2,3,4,5,6,7,8,9,10)

Solution

  • You seem to want permutations, not combinations. Try the function permn() from the package combinat:

    # Your first example:
    combinat::permn(c(1, 2, 3))
    #> [[1]]
    #> [1] 1 2 3
    #> 
    #> [[2]]
    #> [1] 1 3 2
    #> 
    #> [[3]]
    #> [1] 3 1 2
    #> 
    #> [[4]]
    #> [1] 3 2 1
    #> 
    #> [[5]]
    #> [1] 2 3 1
    #> 
    #> [[6]]
    #> [1] 2 1 3
    
    # Your second example
    res <- combinat::permn(c(1,2,3,4,5,6,7,8,9,10))
    

    It does take a while, though. And of course the object itself is going to be large:

    system.time(res <- combinat::permn(c(1,2,3,4,5,6,7,8,9,10)))
    #>   user  system elapsed 
    #>  14.661   0.448  15.346 
    pryr::object_size(res)
    #> 639 MB