title: Arrange multiple columns with characters by frequency using fct_infreq()
body: <p>I would like to arrange multiple columns, both of which contain characters, using <a href="https://forcats.tidyverse.org/reference/fct_inorder.html" rel="nofollow noreferrer"><code>forcats::fct_infreq()</code></a> in <a href="https://dplyr.tidyverse.org/reference/arrange.html" rel="nofollow noreferrer"><code>dplyr::arrange()</code></a>. Here, I would like to sort column <code>z</code> and <code>x</code> of a data frame <code>df</code> by their frequency. I can manage to sort column <code>z</code> but I cannot manipulate column <code>x</code> properly.</p>
  <h1>MWE</h1>
  <pre class="lang-r prettyprint-override"><code>#### To load tibble, dplyr, and forcats ####
  library(tidyverse)
  
  #### data creation ####
  df &lt;- tibble(
    x = c(
      &quot;are&quot;, &quot;kore&quot;, &quot;sore&quot;, &quot;are&quot;, &quot;kore&quot;, &quot;are&quot;, &quot;kore&quot;,
      &quot;sore&quot;, &quot;sore&quot;, &quot;kore&quot;, &quot;kore&quot;, &quot;kore&quot;, &quot;kore&quot;, &quot;kore&quot;
      ), 
    y = c(
      2, 1, 2, 3, 2, 3, 1, 
      2, 2, 1, 1, 1, 1, 1
      ), 
    z = c(
      &quot;foo&quot;, &quot;foo&quot;, &quot;foo&quot;, &quot;bar&quot;, &quot;bar&quot;, &quot;bar&quot;, &quot;baz&quot;,
      &quot;baz&quot;, &quot;baz&quot;, &quot;qux&quot;, &quot;qux&quot;, &quot;qux&quot;, &quot;qux&quot;, &quot;qux&quot;
      )
  )
  
  df %&gt;% 
    arrange(
      fct_infreq(z), # only column z is sorted
      fct_infreq(x)  # column x is not desirably sorted 
    )
  </code></pre>
  <h1>Output of the code above</h1>
  <pre class="lang-r prettyprint-override"><code># A tibble: 14 x 3
     x         y z    
     &lt;chr&gt; &lt;dbl&gt; &lt;chr&gt;
   1 kore      1 qux  
   2 kore      1 qux  
   3 kore      1 qux  
   4 kore      1 qux  
   5 kore      1 qux  
   6 kore      2 bar  #  &lt;- `are` is more frequent in the group of `bar` than `kore`
   7 are       3 bar  #
   8 are       3 bar  #
   9 kore      1 baz  ## &lt;- `sore` is more frequent in the group of `baz` than `kore`
  10 sore      2 baz  ##
  11 sore      2 baz  ##
  12 kore      1 foo  
  13 are       2 foo  
  14 sore      2 foo  
  </code></pre>
  <h1>Desirable output</h1>
  <pre class="lang-r prettyprint-override"><code># A tibble: 14 x 3
     x         y z    
     &lt;chr&gt; &lt;dbl&gt; &lt;chr&gt;
   1 kore      1 qux  
   2 kore      1 qux  
   3 kore      1 qux  
   4 kore      1 qux  
   5 kore      1 qux  
   6 are       3 bar  #  &lt;- `are` is more frequent in the group of `bar` than `kore`
   7 are       3 bar  #
   8 kore      2 bar  #
   9 sore      2 baz  ## &lt;- `sore` is more frequent in the group of `baz` than `kore`
  10 sore      2 baz  ##
  11 kore      1 baz  ##
  12 kore      1 foo  
  13 are       2 foo  
  14 sore      2 foo  
  </code></pre>
  <p>How should I achieve this using <a href="https://forcats.tidyverse.org/reference/fct_inorder.html" rel="nofollow noreferrer"><code>forcats::fct_infreq()</code></a> and <a href="https://dplyr.tidyverse.org/reference/arrange.html" rel="nofollow noreferrer"><code>dplyr::arrange()</code></a>?</p>