title: dplyr window functions with order_by and with_order
body: <p>BACKGROUD</p>
  
  <p>dplyr has window functions. When you want to control the order of window functions,
  you can use <code>order_by</code>.</p>
  
  <p>DATA</p>
  
  <pre><code>mydf &lt;- data.frame(id = c("ana", "bob", "caroline",
                            "bob", "ana", "caroline"),
                     order = as.POSIXct(c("2015-01-01 18:00:00", "2015-01-01 18:05:00",
                                          "2015-01-01 19:20:00", "2015-01-01 09:07:00",
                                          "2015-01-01 08:30:00", "2015-01-01 11:11:00"),
                                          format = "%Y-%m-%d %H:%M:%S"),  
                     value = runif(6, 10, 20),
                     stringsAsFactors = FALSE)
  
  #        id               order    value
  #1      ana 2015-01-01 18:00:00 19.00659
  #2      bob 2015-01-01 18:05:00 13.64010
  #3 caroline 2015-01-01 19:20:00 12.08506
  #4      bob 2015-01-01 09:07:00 14.40996
  #5      ana 2015-01-01 08:30:00 17.45165
  #6 caroline 2015-01-01 11:11:00 14.50865
  </code></pre>
  
  <p>Suppose you want to use <code>lag()</code>, you can do the following.</p>
  
  <pre><code>arrange(mydf, id, order) %&gt;%
  group_by(id) %&gt;%
  mutate(check = lag(value))
  
  #        id               order    value    check
  #1      ana 2015-01-01 08:30:00 17.45165       NA
  #2      ana 2015-01-01 18:00:00 19.00659 17.45165
  #3      bob 2015-01-01 09:07:00 14.40996       NA
  #4      bob 2015-01-01 18:05:00 13.64010 14.40996
  #5 caroline 2015-01-01 11:11:00 14.50865       NA
  #6 caroline 2015-01-01 19:20:00 12.08506 14.50865
  </code></pre>
  
  <p>However, you can avoid using <code>arrange()</code> with <code>order_by()</code>.</p>
  
  <pre><code>group_by(mydf, id) %&gt;%
  mutate(check = lag(value, order_by = order))
  
  #        id               order    value    check
  #1      ana 2015-01-01 18:00:00 19.00659 17.45165
  #2      bob 2015-01-01 18:05:00 13.64010 14.40996
  #3 caroline 2015-01-01 19:20:00 12.08506 14.50865
  #4      bob 2015-01-01 09:07:00 14.40996       NA
  #5      ana 2015-01-01 08:30:00 17.45165       NA
  #6 caroline 2015-01-01 11:11:00 14.50865       NA
  </code></pre>
  
  <p>EXPERIMENT</p>
  
  <p>I wanted to apply the same procedure to the case in which I wanted
  to assign row number to a new column. Using the sample data, you can do the folowing.</p>
  
  <pre><code>group_by(mydf, id) %&gt;%
  arrange(order) %&gt;%
  mutate(num = row_number())
  
  #        id               order    value num
  #1      ana 2015-01-01 08:30:00 17.45165   1
  #2      ana 2015-01-01 18:00:00 19.00659   2
  #3      bob 2015-01-01 09:07:00 14.40996   1
  #4      bob 2015-01-01 18:05:00 13.64010   2
  #5 caroline 2015-01-01 11:11:00 14.50865   1
  #6 caroline 2015-01-01 19:20:00 12.08506   2
  </code></pre>
  
  <p>Can we omit the arrange line? Seeing the CRAN manual, I did the following.
  Both attempts were not successful.</p>
  
  <pre><code>### Not working
  group_by(mydf, id) %&gt;%
  mutate(num = row_number(order_by = order))
  
  ### Not working
  group_by(mydf, id) %&gt;%
  mutate(num = order_by(order, row_number()))
  </code></pre>
  
  <p>How can we achieve this?</p>