Search code examples
rcombinations

Create list with all possible combinations from another list in R


Create list with all possible combinations from another list in R

Ive seen people ask this question but only found threads with answers for other software and not I R

Basically I just want to create a list with all unique combinations possible

Desired input

a, b, c 

desired output

a, ab, ac, b, bc, c, abc 

Solution

  • One possibility

    > x=letters[1:3] 
    > rapply(sapply(seq_along(x),function(y){combn(x,y,simplify=F)}),paste0,collapse="")
    [1] "a"   "b"   "c"   "ab"  "ac"  "bc"  "abc"