Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 619
Dennis Zaragosa Avatar asked Apr 01 '15 09:04

Dennis Zaragosa


People also ask

How to generate all distinct strings from a given index?

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.

How many distinct permutations of the given string are there?

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

Why don’t we swap the index of a string while generating permutations?

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.


Video Answer


1 Answers

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.

like image 69
David Arenburg Avatar answered Oct 21 '22 04:10

David Arenburg