title: R: all combinations of all lengths from a vector of elements each with 2 conditions
body: <p>Following up on <a href="https://stackoverflow.com/questions/64040588/r-all-possible-combinations-from-a-vector-of-elements-with-2-possible-condition/64048001#64048001">this question I posted some days ago</a>, I would like to extend the same case for combinations of every length.</p>
  <p>So I have a vector of the form:</p>
  <pre><code>markers &lt;- LETTERS[1:5]
  </code></pre>
  <p>Originally I just wanted all possible combinations of conditions <code>+</code> and <code>-</code> for all the <em>markers</em>; i.e &quot;lowest hierarchy level&quot; of combinations of 5.</p>
  <p>So applying the answer to the above question, I obtained the following:</p>
  <pre><code> [1] &quot;A+/B+/C+/D+/E+&quot; &quot;A-/B+/C+/D+/E+&quot; &quot;A+/B-/C+/D+/E+&quot; &quot;A-/B-/C+/D+/E+&quot; &quot;A+/B+/C-/D+/E+&quot; &quot;A-/B+/C-/D+/E+&quot; &quot;A+/B-/C-/D+/E+&quot;
   [8] &quot;A-/B-/C-/D+/E+&quot; &quot;A+/B+/C+/D-/E+&quot; &quot;A-/B+/C+/D-/E+&quot; &quot;A+/B-/C+/D-/E+&quot; &quot;A-/B-/C+/D-/E+&quot; &quot;A+/B+/C-/D-/E+&quot; &quot;A-/B+/C-/D-/E+&quot;
  [15] &quot;A+/B-/C-/D-/E+&quot; &quot;A-/B-/C-/D-/E+&quot; &quot;A+/B+/C+/D+/E-&quot; &quot;A-/B+/C+/D+/E-&quot; &quot;A+/B-/C+/D+/E-&quot; &quot;A-/B-/C+/D+/E-&quot; &quot;A+/B+/C-/D+/E-&quot;
  [22] &quot;A-/B+/C-/D+/E-&quot; &quot;A+/B-/C-/D+/E-&quot; &quot;A-/B-/C-/D+/E-&quot; &quot;A+/B+/C+/D-/E-&quot; &quot;A-/B+/C+/D-/E-&quot; &quot;A+/B-/C+/D-/E-&quot; &quot;A-/B-/C+/D-/E-&quot;
  [29] &quot;A+/B+/C-/D-/E-&quot; &quot;A-/B+/C-/D-/E-&quot; &quot;A+/B-/C-/D-/E-&quot; &quot;A-/B-/C-/D-/E-&quot;
  </code></pre>
  <p>Now I want to extend this to &quot;upper hierarchy&quot; levels of combinations of 1, 2, 3, and 4 <em>markers</em>. So I would get something like:</p>
  <pre><code>&quot;A+&quot;
  &quot;A-&quot;
  &quot;B+&quot;
  &quot;B-&quot;
  &quot;C+&quot;
  &quot;C-&quot;
  ...
  &quot;A+/B+&quot;
  &quot;A-/B+&quot;
  &quot;A+/B-&quot;
  &quot;A-/B-&quot;
  &quot;B+/C+&quot;
  &quot;B+/C-&quot;
  &quot;B-/C+&quot;
  &quot;B-/C-&quot;
  ...
  &quot;A+/B+/C+&quot;
  &quot;A-/B+/C+&quot;
  ...
  &quot;A+/B+/C+/D+/E+&quot;
  &quot;A-/B+/C+/D+/E+&quot;
  &quot;A+/B-/C+/D+/E+&quot;
  &quot;A-/B-/C+/D+/E+&quot;
  &quot;A+/B+/C-/D+/E+&quot;
  ...
  </code></pre>
  <p>What would be the fastest optimal way to build on top of the accepted answer to the previous question?</p>
  <p>It doesn't have to be done in one shot, it would still be ok (or even better), to get the &quot;inner nodes&quot; from the previous results of groups of 5. Maybe working on the <code>expand.grid</code> intermediate result.</p>
  <p>Any idea? Thanks!</p>
  <p><em><strong>EDIT</strong></em></p>
  <p>The best way for my intentions would be to actually keep a place holder for all the <em>markers</em> in the higher hierarchy combinations.</p>
  <p>So for example in this case <code>A+/D-</code> would become <code>A+/NA/NA/D-/NA</code></p>
  <p><em><strong>EDIT 2</strong></em></p>
  <p>Even the first answer to create all the possible n-size combinations (including <code>NA</code>) from scratch is really good... in my real world scenario I have the chance to retrieve a much smaller filtered list of the &quot;lowest hierarchy level&quot; combinations of 5 &quot;markers&quot; that I would be most interested in.</p>
  <p>In this scenario, it would be really good to have the option to extract the &quot;upper level nodes&quot; of combinations of 1,2,3,4...n (with <code>NA</code>) from that filtered list (instead of generating all possible n-size combinations from scratch)...</p>
  <p>Any idea?</p>