title: Calculate cumulative sum after a set period of time
body: <p>I have a data frame with COVID data and I'm trying to make a column calculating the number of recovered people based off of the number of positive tests.</p>
  <p>My data has a location, a date, and the number of tests administered/positive results/negative results each day. Here's a few lines using one location as an example (the real data has several months worth of dates):</p>
  <pre><code>loc    date        tests  pos  neg  active
  spot1  2020-04-10  1      1    0    5
  spot1  2020-04-11  2      1    1    6
  spot1  2020-04-12  0      0    0    6
  spot1  2020-04-13  11     1    10   7
  </code></pre>
  <p>I want to make a new column that cumulatively counts each positive test in each location 14 days after it is recorded. On 2020-04-24, the 5 active classes are not active anymore, so I want a recovered column with 5. For each date I want the newly nonactive cases to be added.</p>
  <p>My first thought was to try it in a loop:</p>
  <pre><code>df1 &lt;- df %&gt;%
    mutate(date = as.Date(date)) %&gt;%
    group_by(loc) %&gt;%
    mutate(rec = for (i in 1:nrow(df)) {
    #getting number of new cases
    x &lt;- df$pos[i]
    #add 14 days to the date
    d &lt;- df$date + 14
    df$rec &lt;- sum(x)
  })
  </code></pre>
  <p>As you can see, I'm not the best at writing for loops. That gives me a bunch of numbers, but bear very little meaningful relationship to the data.</p>
  <p>Also tried it with map_dbl:</p>
  <pre><code>df1 &lt;- df %&gt;%
    mutate(date = as.Date(date)) %&gt;%
    group_by(loc) %&gt;%
    mutate(rec = map_dbl(date, ~sum(pos[(date &lt;= . + 14) &amp; date &gt;= .])))
  </code></pre>
  <p>Which resulted in the same number printed on the entire rec column.</p>
  <p>Any suggestions? (Sorry for the lengthy explanation, just want to make sure this all makes sense)</p>