Unique string combinations
I have a vector which contains certain words
colors<-c("Yellow","Blue","Red")
> colors
[1] "Yellow" "Blue" "Red"
Now I want to create a new variable, colorsCombined, in which the original vector is present and also all possible combinations of those words.
> colorsCombined
[1] "Yellow", "Blue", "Red", "YellowBlue", "YellowRed", "BlueRed", "YellowBlueRed"
I consider YellowBlue to be the same as BlueYellow.
How do I do this?
Solution 1:
One option is to run the combn
function in a lapply
loop. You can define it as your own function
allCombs <- function(x) c(x, lapply(seq_along(x)[-1L],
function(y) combn(x, y, paste0, collapse = "")),
recursive = TRUE)
allCombs(colors)
## [1] "Yellow" "Blue" "Red" "YellowBlue" "YellowRed" "BlueRed" "YellowBlueRed"
Explanation (per request)
Basically OP wants a vector of all possible combinations (not permutations) depending on the length of the input vector. Thus, we should run the combn
function over k <- 1:length(x)
and generate all combinations for every k
.
So when k == 1
, it's just the original vector, so we can skip that part and just concatenate the original vector using c
. The rest of the generated combinations return a list of vectors with different lengths (in a descending order for obvious reasons), thus we need to use recursive = TRUE
within the c
function in order to mimic the behaviour of unlist
and combine the results into a single vector.