title: Reorder factor levels within group
body: <p>I would like to reorder the levels of a factor in one column, but within groups defined by a grouping column.</p>
  
  <p>Simple example data set:</p>
  
  <pre><code>df &lt;- structure(list(a_factor = structure(1:6, .Label = c("a", "b", 
  "c", "d", "e", "f"), class = "factor"), group = structure(c(1L, 
  1L, 1L, 2L, 2L, 2L), .Label = c("group1", "group2"), class = "factor"), 
  value = 1:6), class = "data.frame", row.names = c(NA, -6L
  ))
  
  &gt; df
    a_factor  group value
  1        a group1     1
  2        b group1     2
  3        c group1     3
  4        d group2     4
  5        e group2     5
  6        f group2     6
  </code></pre>
  
  <p>More precisely, how do I reorder the factor levels, e.g. descending by <code>value</code> where <code>df$group == "group1"</code>, but ascending by <code>value</code> where <code>df$group == "group2"</code>, preferably in dplyr?</p>
  
  <p>An expected output might be:</p>
  
  <pre><code>&gt; df
    a_factor  group value
  1        c group1     3
  2        b group1     2
  3        a group1     1
  4        d group2     4
  5        e group2     5
  6        f group2     6
  </code></pre>
  
  <p>Although, the question is more generally about how to tackle this in dplyr. </p>