I'm trying to build a list/vector/... from something like this:
elements <- c("a","b","c")
that gives me combinations of every entry of elements
with all other entries of elements
. These combinations should go from length 1
to length(elements)
. The result should therefore (if I haven't missed a combination) look like this:
combinationsvector <- c("a","b","c", "ab", "ac", "bc", "abc")
or:
combinationslist <- list("a","b","c", "ab", "ac", "bc", "abc")
I don't want to distinguish between "ab"
and "ba"
, those are the same thing for me. This should result in {2^K}-1
combinations (I don't need the empty entry but it wouldn't hurt either). This is for a model averaging exercise and I'm trying to get all possible variable combinations of my dataset. In reality, elements
is larger than 3.