Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a brute-force-and-dirty solution, which may work for different number of groups, but you really should test it before use. Moreover, as it uses <code>permn</code>, it will be unusable very fast depending on the size of your vector :</p> <pre><code>library(combinat) split.groups &lt;- function(x, nb.groups) { length.groups &lt;- length(x)/nb.groups perm &lt;- permn(x) perm &lt;- lapply(perm, function(v) { m &lt;- as.data.frame(matrix(v, length.groups, nb.groups)) m &lt;- apply(m,2,sort) m &lt;- t(m) m &lt;- m[order(m[,1]),] rownames(m) &lt;- NULL m}) unique(perm) } </code></pre> <p>Which gives, for example :</p> <pre><code>R&gt; split.groups(1:4, 2) [[1]] [,1] [,2] [1,] 1 2 [2,] 3 4 [[2]] [,1] [,2] [1,] 1 4 [2,] 2 3 [[3]] [,1] [,2] [1,] 1 3 [2,] 2 4 </code></pre> <p>Or :</p> <pre><code>R&gt; split.groups(1:6, 3) [[1]] [,1] [,2] [1,] 1 2 [2,] 3 4 [3,] 5 6 [[2]] [,1] [,2] [1,] 1 2 [2,] 3 6 [3,] 4 5 [[3]] [,1] [,2] [1,] 1 6 [2,] 2 3 [3,] 4 5 [[4]] [,1] [,2] [1,] 1 2 [2,] 3 5 [3,] 4 6 [[5]] [,1] [,2] [1,] 1 6 [2,] 2 5 [3,] 3 4 [[6]] [,1] [,2] [1,] 1 5 [2,] 2 6 [3,] 3 4 [[7]] [,1] [,2] [1,] 1 5 [2,] 2 3 [3,] 4 6 [[8]] [,1] [,2] [1,] 1 5 [2,] 2 4 [3,] 3 6 [[9]] [,1] [,2] [1,] 1 6 [2,] 2 4 [3,] 3 5 [[10]] [,1] [,2] [1,] 1 4 [2,] 2 3 [3,] 5 6 [[11]] [,1] [,2] [1,] 1 4 [2,] 2 6 [3,] 3 5 [[12]] [,1] [,2] [1,] 1 4 [2,] 2 5 [3,] 3 6 [[13]] [,1] [,2] [1,] 1 3 [2,] 2 5 [3,] 4 6 [[14]] [,1] [,2] [1,] 1 3 [2,] 2 6 [3,] 4 5 [[15]] [,1] [,2] [1,] 1 3 [2,] 2 4 [3,] 5 6 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload