title: dplyr equivalent of sql row_number() over (partition by group order by value)
body: <p><strong>Initial situation</strong></p>
  
  <p>I have a data set of the following form:</p>
  
  <pre><code>library(dplyr)
  
  dat &lt;- tribble(
    ~name, ~iq,
    "ben",  100,
    "alex",  98,
    "mia",  110,
    "paco", 124,
    "mia",  112,
    "mia",  120,
    "paco", 112,
    "ben",   90,
    "alex", 107
  )
  </code></pre>
  
  <p>I'd like to create a new column which ranks, grouped by <code>name</code>, the values <code>iq</code> in descending order. In SQL one could write</p>
  
  <pre><code>select
    name,
    iq,
    row_number() over (partition by name order by iq desc) as rank
  from
    dat;
  </code></pre>
  
  <p>which would produce the following expected output (already ordered for simplicity):</p>
  
  <pre class="lang-r prettyprint-override"><code>#&gt;   name     iq  rank
  #&gt;   &lt;chr&gt; &lt;dbl&gt; &lt;int&gt;
  #&gt; 1 alex    107     1
  #&gt; 2 alex     98     2
  #&gt; 3 ben     100     1
  #&gt; 4 ben      90     2
  #&gt; 5 mia     120     1
  #&gt; 6 mia     112     2
  #&gt; 7 mia     110     3
  #&gt; 8 paco    124     1
  #&gt; 9 paco    112     2
  </code></pre>
  
  <p><strong>Questions</strong></p>
  
  <p>With my data, one can achieve the desired result with:</p>
  
  <pre><code>dat %&gt;%
    group_by(name) %&gt;%
    mutate(rank = with_order(order_by = iq,
                             fun      = row_number, 
                             x        = desc(iq)
                             )
           ) %&gt;% 
    arrange(name, rank)
  
  #&gt; # A tibble: 9 x 3
  #&gt; # Groups:   name [4]
  #&gt;   name     iq  rank
  #&gt;   &lt;chr&gt; &lt;dbl&gt; &lt;int&gt;
  #&gt; 1 alex    107     1
  #&gt; 2 alex     98     2
  #&gt; 3 ben     100     1
  #&gt; 4 ben      90     2
  #&gt; 5 mia     120     1
  #&gt; 6 mia     112     2
  #&gt; 7 mia     110     3
  #&gt; 8 paco    124     1
  #&gt; 9 paco    112     2
  </code></pre>
  
  <p>However, I don't understand <em>why</em> the code works. When reading the <a href="https://www.rdocumentation.org/packages/dplyr/versions/0.7.8/topics/with_order" rel="noreferrer">documentation</a> of <code>dplyr::with_order()</code>, it says the arguments are</p>
  
  <ul>
  <li><code>order_by</code> = the vector to order by</li>
  <li><code>fun</code>      = window function</li>
  <li><code>x, ...</code>   = arguments to <code>f</code></li>
  </ul>
  
  <p>Given the description in the documentation and the working code, I have two questions I cannot answer:</p>
  
  <ol>
  <li>What is the purpose of the argument <code>x</code>? Why not just specify the vector to order by and the window function (like in sql)? What is meant by <code>f</code>?</li>
  <li>Why don't I have to write <code>order_by = desc(iq)</code>? To get the result I expect I have to write <code>x = desc(iq)</code> and set <code>order_by = iq</code>. This seems to contradict the documentation, which states that <code>order_by</code> = the vector to order by.</li>
  </ol>