title: How to create a variable that is the sum of consecutive rows within a given time frame and by id
body: <p>I am trying to implement a sum of consecutive values that fall with 365 days of each other, grouped by a unique identifier in R. For example, for date 1 of a particular ID, we would add dates 2,3,4 (fall within 365 days) of the same ID to get a total cost for date 1. Then for date 2, we would add 3 and 4 to get that total cost and so on. I have tried several rolling sums (<a href="https://stackoverflow.com/questions/30153835/r-dplyr-rolling-sum">R dplyr rolling sum</a>) and similar solutions from dplyr that take sum consecutive values (<a href="https://stackoverflow.com/questions/57928394/calculate-sum-of-a-column-if-the-difference-between-consecutive-rows-meets-a-con">Calculate sum of a column if the difference between consecutive rows meets a condition</a>) with certain constraints but could not get the code to distinguish the amount of days. I have included a sample dataset with a solution dataset for an example that I am looking for.</p>
  <p>Starting Data Set</p>
  <pre><code>ID &lt;- c(1,1,1,1,1,1,2,2,2,2,3)
  admitdt &lt;-c(&quot;2014-10-19&quot;,&quot;2014-10-24&quot;,&quot;2015-01-31&quot;,&quot;2016-01-20&quot;,&quot;2017-06-30&quot;,&quot;2017-07-17&quot;,&quot;2015-04-21&quot;,&quot;2015-04-22&quot;,&quot;2015-05-04&quot;,&quot;2015-07-25&quot;,&quot;2014-11-11&quot;)
  cost&lt;-c(2000,14077,5000,200,560,5000,888,5959,1819,7508,6406)
  cost365&lt;-c(21077,19077,5200,200,5560,5000,16174,15286,9327,7508,6406)
  df2&lt;-data.frame(ID,admitdt,cost,cost365)
  
    ID    admitdt  cost
  1   1 2014-10-19  2000
  2   1 2014-10-24 14077
  3   1 2015-01-31  5000
  4   1 2016-01-20   200
  5   1 2017-06-30   560
  6   1 2017-07-17  5000
  7   2 2015-04-21   888
  8   2 2015-04-22  5959
  9   2 2015-05-04  1819
  10  2 2015-07-25  7508
  11  3 2014-11-11  6406
  </code></pre>
  <p>Solution:</p>
  <pre><code>ID &lt;- c(1,1,1,1,1,1,2,2,2,2,3)
  admitdt &lt;-c(&quot;2014-10-19&quot;,&quot;2014-10-24&quot;,&quot;2015-01-31&quot;,&quot;2016-01-20&quot;,&quot;2017-06-30&quot;,&quot;2017-07-17&quot;,&quot;2015-04-21&quot;,&quot;2015-04-22&quot;,&quot;2015-05-04&quot;,&quot;2015-07-25&quot;,&quot;2014-11-11&quot;)
  cost&lt;-c(2000,14077,5000,200,560,500,888,5959,1819,7508,6406)
  cost365&lt;-c(21077,19077,5200,200,5560,5000,16174,15286,9327,7508,6406)
  df2&lt;-data.frame(ID,admitdt,cost,cost365)
    ID    admitdt  cost cost365
  1   1 2014-10-19  2000   21077
  2   1 2014-10-24 14077   19077
  3   1 2015-01-31  5000    5200
  4   1 2016-01-20   200     200
  5   1 2017-06-30   560    5560
  6   1 2017-07-17  5000    5000
  7   2 2015-04-21   888   16174
  8   2 2015-04-22  5959   15286
  9   2 2015-05-04  1819    9327
  10  2 2015-07-25  7508    7508
  11  3 2014-11-11  6406    6406
  </code></pre>