title: Sort data.frame on one categorical column but alternate according to pattern in R
body: <p>I have two possible types of datasets:</p>
  <pre><code>test6 &lt;- data.frame(S=c(&quot;B&quot;,&quot;Z&quot;,&quot;B&quot;,&quot;Z&quot;,&quot;B&quot;,&quot;Z&quot;,&quot;B&quot;,&quot;B&quot;,&quot;B&quot;,&quot;Z&quot;,&quot;Z&quot;,&quot;Z&quot;),w=c(1,1.2,1.3,2,0.9,0.95,1,1.5,1,1.1,0.8,1.3))
  test5 &lt;- data.frame(S=c(&quot;B&quot;,&quot;Z&quot;,&quot;B&quot;,&quot;Z&quot;,&quot;B&quot;,&quot;Z&quot;,&quot;B&quot;,&quot;B&quot;,&quot;Z&quot;,&quot;Z&quot;),w=c(1,1.2,1.3,2,0.9,0.95,1,1.5,1.1,0.8))
  </code></pre>
  <p>I want to order them to get this end result,
  for test6:</p>
  <pre><code>   S    w
  1  B 1.00
  3  B 1.30
  5  B 0.90
  2  Z 1.20
  4  Z 2.00
  6  Z 0.95
  7  B 1.00
  8  B 1.50
  9  B 1.00
  10 Z 1.10
  11 Z 0.80
  12 Z 1.30
  </code></pre>
  <p>For test5:</p>
  <pre><code>   S    w
  1  B 1.00
  3  B 1.30
  5  B 0.90
  2  Z 1.20
  4  Z 2.00
  7  B 1.00
  8  B 1.50
  6  Z 0.95
  9  Z 1.10
  10 Z 0.80
  </code></pre>
  <p>So getting an alternate sorting with 3 B followed by 3 Z in test6 case, and 3 B followed by 2 Z, then 2 B followed by 3 Z and so on.
  I found one way to do it with:</p>
  <pre><code>library(groupdata2)
  fold(test6, k = 2,method=&quot;n_dis&quot;,cat_col = &quot;S&quot;) 
  </code></pre>
  <p>Which creates groups that I can then sort on to achieve this result but this only works in the test6 case i.e same counts of S categories in each group.
  Anyone have better, more simple ideas? Thanks in advance!</p>