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?
i = 0 1 2 3 A B A C index = 0, s [0] = A Start swapping s [index] with s [i] following it: i = index + 1 = 1 Since s [index] != s [i], swap and recur. i = 2, s [index] == s [i], don't swap i = 3, s [index] != s [i], swap and recur. Below code does the same. Generate all distinct strings is to simply use some if conditions.
We first sort the given string and then apply the below code. aabc aacb abac abca acba acab baac baca bcaa caba caab cbaa Total distinct permutations = 12
While generating permutations, let’s say we are at index = 0, swap it with all elements after it. When we reach i=2, we see that in the string s [index…i-1], there was an index that is equal to s [i]. Thus, swapping it will produce repeated permutations. Thus, we don’t swap it. The below explains it better.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With