title: How to inter cumulative sums between two dates and period for individual days?
body: <p>I would like to extrapolate the sums between two days into daily values. The counts represent the cumulative sums collected up to certain day: Let's say, at day X there is XX apples fallen on the ground. How many apples fell on the ground daily and per different trees? The average apples a day to certain date (DOY) should equal the number of trees in DOY divided by number of previous days without any record (NA).</p>
  <p>In R, I think I need to first recreate the full date range I wish to recalculate the daily values into (<code>2:20</code>). Then merge it with original data. But how to do the interpolate between the dates? The beginning would be then 0 count at DOY 2 (beggining of time series) interpolated until the counts on the first recorded DOY. The fnal values can be only NA, as I do not have any data there (and I can't assume there will be increasing apples of falling apples at the end of season, rather there will be less of them). Scale 2:20 is only used here to understand the process. My original data extends to DOY 270.</p>
  <p>Here is my dummy example:</p>
  <pre><code>dd &lt;- data.frame(cumsum = c(4, 12, 14.5,
                             8, 15, 20,
                             10, 20, 40),
                   doy = c(4, 10, 15, # DOY = day of the year
                           5, 11, 14,
                           5, 10, 15),
                   gp = rep(c('a', 'b', 'd'), each = 3)) # group
  
  doy_df = data.frame(doy = rep(c(2:20), 3),
                      gp = rep(c('a', 'b', 'd'), each = 19))
  
  # complete the df by wished doy
  dd %&gt;% 
    right_join(doy_df) %&gt;% 
    arrange(gp, doy)
  </code></pre>
  <p>How to get the daily value, while accunting for different groups? Here is an <a href="https://machinelearningmastery.com/resample-interpolate-time-series-data-python/" rel="nofollow noreferrer">example in python</a>, but how to make it in R?</p>
  <p>Desired outcome:</p>
  <pre><code>  doy_val doy gp
  1      2      2  a
  2      4      3  a
  3      6      4  a    # counted doy
  4      7      5  a
  5      8      6  a
  6      9      7  a
  7      10     8  a
  8      11     9  a
  9      12    10  a  # counted doy
  10     12.5  11  a
  11     13    12  a
  12     13.5  13  a
  13     14    14  a
  14     14.5  15  a  # counted doy
  15     NA    16  a
  16     NA    17  a
  17     NA    18  a
  ....
  </code></pre>