title: r - arrange values in a dataframe, within a group, based on a variable, in ascending or descending order
body: <p>Got stuck with what I think is a very simple solve, but somehow I cannot find a way.</p>
  
  <p>I'm trying to <code>dplyr::arrange</code> values in a <code>dataframe</code>. The thing is I want to apply the function on groups using <code>dplyr::group_by</code>, and decide whether it is in ascending or descending order based on a variable inside. </p>
  
  <p>A simple example:</p>
  
  <pre><code>library(dplyr)
  
  test &lt;- 
      data.frame(
          var1 = c(rep(1,5), rep(2,5)),
          var2 = c(1,3,2,5,9,7,8,3,5,9),
          var3 = c(rep("i", 5), rep("d", 5))
      )
  </code></pre>
  
  <p>Here I try to arrange by a grouping variable <code>var1</code>, but everything is in ascending order:</p>
  
  <pre><code>test %&gt;% 
      group_by(var1) %&gt;% 
      arrange(var2, .by_group = T)
  </code></pre>
  
  <p>The goal is to automate arranging based on <code>var3</code> values. More specifically, by ascending order if the <code>var3</code> is "i" and in descending order if it is "d", but within the groups.</p>
  
  <p>Desired result:</p>
  
  <pre><code>data.frame(
      var1 = c(rep(1,5), rep(2,5)),
      var2 = c(1,2,3,5,9,9,8,7,5,3),
      var3 = c(rep("i", 5), rep("d", 5)))
  </code></pre>