0

Please suppose I have the following vector:

vec = c("A", "B", "C")

I want to be able to produce a vector to have the following output:

vec_combn = c("", "A", "B", "C", "A+B", "A+C", "B+C", "A+B+C")

This is all possible combinations including the empty set.

Is there any way to do this quickly?

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

1 Answers1

1

Try:

unlist(sapply(0:length(vec),function(n) apply(combn(vec,n),2,function(v) paste0(v,collapse="+"))))

[1] ""      "A"     "B"     "C"     "A+B"   "A+C"   "B+C"   "A+B+C"
Waldi
  • 39,242
  • 6
  • 30
  • 78