Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201867

All combinations of elements of vectors at varying length with conditions to only pick one element per vector and disregarding order?

$
0
0

Imagine that we have three vectors:

v_1 = c("a", "b", "c", "d")
v_2 = c("e", "f", "g")
v_3 = c("h", "i")

I'd like to use R to retrieve a complete list of all combinations of these three vectors for all possible lengths under the condition that:

  • we can pick a maximum of 1 element per vector (e.g., the combination "ab" shall be disregarded since they both belong to v_1)
  • order is not important (e.g., the combination "ae" and "ea" shall only be counted once).

Given that I have done the math correctly, we should end up with 59 distinct combinations of lengths 1 to 3. I've looked into the combn() function, but it seems difficult (or impossible?) to apply the the two conditions. Does anyone a have suggestion for how to solve this in R?

Thanks!


Viewing all articles
Browse latest Browse all 201867

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>