Reputation: 31
a<-c('a','b','c','d')
Need this to be segregated in combinations
Required output is
(a b c d,a b c,b c d,a b,b c,c d,a,b,c,d)
Upvotes: 2
Views: 171
Reputation: 7597
The desired output can be built from the power set of c('a', 'b', 'c', 'd') = letters[1:4]
. Using the powerset
function from the rje
library, we have:
unlist(lapply(rje::powerSet(letters[1:4])[-1], paste0, collapse = ' '))
[1] "a" "b" "a b" "c" "a c" "b c" "a b c"
[8] "d" "a d" "b d" "a b d" "c d" "a c d" "b c d"
[15] "a b c d"
Upvotes: 0
Reputation: 1233
I used @chinsoon12 suggestions with a further paste
to get your required output:
paste(lapply(unlist(lapply(rev(seq_along(a)), function(m) combn(a, m, simplify=FALSE)), recursive=FALSE), paste, collapse=" "), collapse=",")
Returns: "a b c d,a b c,a b d,a c d,b c d,a b,a c,a d,b c,b d,c d,a,b,c,d"
Upvotes: 1