How to create a list of an unordered combination of elements in a string vector in R?

R ProgrammingServer Side ProgrammingProgramming

<p>An unordered combination of elements means that the combination of the values in a way that does not make any particular arrangement. For example, if we have three values one, two, and three then they can be arranged in the following way which is unordered &minus;</p><pre class="result notranslate">&quot;one&quot; &quot;two&quot; &quot;three&quot; &quot;one&quot; &quot;two&quot; &quot;one&quot; three&quot; &quot;two&quot; &quot;three&quot; &quot;one&quot; &quot;two&quot; &quot;three&quot;</pre><h2>Example</h2><p><a class="demo" href="http://tpcg.io/3MCqdlSB" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">x&lt;-c(&quot;India&quot;,&quot;China&quot;,&quot;Russia&quot;,&quot;Canada&quot;) unlist(lapply(seq_along(x),combn,x=x,simplify=FALSE),recursive=FALSE)</pre><h2>Output</h2><pre class="result notranslate">[[1]] [1] &quot;India&quot; [[2]] [1] &quot;China&quot; [[3]] [1] &quot;Russia&quot; [[4]] [1] &quot;Canada&quot; [[5]] [1] &quot;India&quot; &quot;China&quot; [[6]] [1] &quot;India&quot; &quot;Russia&quot; [[7]] [1] &quot;India&quot; &quot;Canada&quot; [[8]] [1] &quot;China&quot; &quot;Russia&quot; [[9]] [1] &quot;China&quot; &quot;Canada&quot; [[10]] [1] &quot;Russia&quot; &quot;Canada&quot; [[11]] [1] &quot;India&quot; &quot;China&quot; &quot;Russia&quot; [[12]] [1] &quot;India&quot; &quot;China&quot; &quot;Canada&quot; [[13]] [1] &quot;India&quot; &quot;Russia&quot; &quot;Canada&quot; [[14]] [1] &quot;China&quot; &quot;Russia&quot; &quot;Canada&quot; [[15]] [1] &quot;India&quot; &quot;China&quot; &quot;Russia&quot; &quot;Canada&quot;</pre><h2>Example</h2><p><a class="demo" href="http://tpcg.io/G7WTiGDi" rel="nofollow" target="_blank">&nbsp;Live Demo</a></p><pre class="prettyprint notranslate">y&lt;-c(&quot;Winter&quot;,&quot;Rainy&quot;,&quot;Summer&quot;) unlist(lapply(seq_along(y),combn,x=y,simplify=FALSE),recursive=FALSE)</pre><h2>Output</h2><pre class="result notranslate">[[1]] [1] &quot;Winter&quot; [[2]] [1] &quot;Rainy&quot; [[3]] [1] &quot;Summer&quot; [[4]] [1] &quot;Winter&quot; &quot;Rainy&quot; [[5]] [1] &quot;Winter&quot; &quot;Summer&quot; [[6]] [1] &quot;Rainy&quot; &quot;Summer&quot; [[7]] [1] &quot;Winter&quot; &quot;Rainy&quot; &quot;Summer&quot;</pre>
raja
Updated on 14-Oct-2020 10:05:56

Advertisements