title: difference from first in group with dplyr
body: <p>I'm trying to create a window function with dplyr, that will return a new vector with the difference between each value and the first of its group.
  For example, given this dataset:</p>
  
  <pre><code>dummy &lt;- data.frame(userId=rep(1,6),
       libId=rep(999,6),
       curatorId=c(1:2,1:2,1:2),
       iterationNum=c(0,0,1,1,2,2),
       rf=c(5,10,0,15,30,40)
  )
  </code></pre>
  
  <p>That creates this dataset:</p>
  
  <pre><code>  userId libId curatorId iterationNum rf
  1      1   999         1            0  5
  2      1   999         2            0 10
  3      1   999         1            1  0
  4      1   999         2            1 15
  5      1   999         1            2 30
  6      1   999         2            2 40
  </code></pre>
  
  <p>And given this grouping:</p>
  
  <pre><code> dummy&lt;-group_by(dummy,libId,userId,curatorId)
  </code></pre>
  
  <p>Would give this result:</p>
  
  <pre><code>  userId libId curatorId iterationNum   rf   rf.diff
  1      1   999         1            0  5    0
  2      1   999         2            0 10    0
  3      1   999         1            1  0   -5
  4      1   999         2            1 15   -5
  5      1   999         1            2 30    25
  6      1   999         2            2 40    30
  </code></pre>
  
  <p>So for each group of users, libs and curators, I would get the rf value, minus the rf value with iterationNum=0.
  I tried playing with the <code>first</code> function, the <code>rank</code> function and others, but couldn't find a way to nail it.</p>
  
  <p>---EDIT---</p>
  
  <p>This is what I tried:</p>
  
  <pre><code>dummy %&gt;% 
    group_by(userId,libId,curatorId) %&gt;% 
    mutate(rf.diff = rf - subset(dummy,iterationNum==0)[['rf']])
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>dummy %&gt;% 
    group_by(userId,libId,curatorId) %&gt;% 
    mutate(rf.diff = rf - first(x = rf,order_by=iterationNum))
  </code></pre>
  
  <p>Which crashes R and returns this error message:</p>
  
  <blockquote>
    <p>pure virtual method called terminate called after throwing an instance
    of 'Rcpp::exception'   what():  incompatible size (%d), expecting %d
    (the group size) or 1`</p>
  </blockquote>