title: pass grouped dataframe to own function in dplyr
body: <p>I am trying to transfer from plyr to dplyr. However, I still can't seem to figure out how to call on own functions in a chained dplyr function.</p>
  
  <p>I have a data frame with a factorised ID variable and an order variable. I want to split the frame by the ID, order it by the order variable and add a sequence in a new column.</p>
  
  <p>My plyr functions looks like this:</p>
  
  <pre><code>f &lt;- function(x) cbind(x[order(x$order_variable), ], Experience = 0:(nrow(x)-1))
  data &lt;- ddply(data, .(ID_variable), f)
  </code></pre>
  
  <p>In dplyr I though this should look something like this</p>
  
  <pre><code>f &lt;- function(x) cbind(x[order(x$order_variable), ], Experience = 0:(nrow(x)-1))
  data &lt;- data %&gt;% group_by(ID_variable) %&gt;% f
  </code></pre>
  
  <p>Can anyone tell me how to modify my dplyr call to successfully pass my own function and get the same functionality my plyr function provides?</p>
  
  <p><strong>EDIT:</strong> If I use the dplyr formula as described here, it DOES pass an object to f. However, while plyr seems to pass a number of different tables (split by the ID variable), dplyr does not pass one table per group but the ENTIRE table (as some kind of dplyr object where groups are annotated), thus when I cbind the Experience variable it appends a counter from 0 to the length of the entire table instead of the single groups.</p>
  
  <p>I have found a way to get the same functionality in dplyr using this approach:</p>
  
  <pre><code>data &lt;- data %&gt;%
      group_by(ID_variable) %&gt;%
      arrange(ID_variable,order_variable) %&gt;% 
      mutate(Experience = 0:(n()-1))
  </code></pre>
  
  <p>However, I would still be keen to learn how to pass grouped variables split into  different tables to own functions in dplyr.</p>