title: Conditional accumulating sum with a dynamic condition
body: <p>Good afternoon
  i'm trying to create an accumulating mean with a "twist" - i only want to average fields that dated before the current one (there may be field with the same date)</p>
  
  <p>i succeeded doing it the "dirty way" using several custom created function but it takes too long and it's very inefficient - im pretty sure that there is a better way.</p>
  
  <p>i was thinking about something along the line of:</p>
  
  <pre><code>averages &lt;- DB %&gt;% group_by(field1,field2) %&gt;% mutate(Avg=cummean(???*value1)))
  </code></pre>
  
  <p>how do i access the current observation for <code>cummean</code>  function</p>
  
  <p>the route i went was to create a logical vector for every subset with a loop</p>
  
  <pre><code>for (i in 1:length(datevector)-1)
      logicalvector[i] &lt;- datevector[length(datevector)]&gt;datevector[i]
    logicalvector[length(datevector)]=F
  </code></pre>
  
  <p>and use that in another function to calculate the mean</p>
  
  <p>a simple example is:</p>
  
  <pre><code>df &lt;- data.frame(id=1:5,Date=as.Date(c("2013-08-02","2013-08-02","2013-08-03","2013-08-03","2013-08-04")),Value=c(1,4,5,2,4))
  
  id  Date    Value     accum mean
  1  02/08/2013     1         0
  2  02/08/2013     4         0
  3  03/08/2013     5        2.5
  4  03/08/2013     2        2.5
  5  04/08/2013     4         3
  
  Explanation:
  there are no observation with a prior date for the first 2 observations so the mean is 0
  the 3rd observation averages the 1st and 2nd, so does the 4th.
  the 5th observation averages all
  </code></pre>