title: Apply cumsum function to a variable with several conditions
body: <p>I am having similar data to this one:</p>
  <pre><code>data &lt;- data.frame (date=seq.Date(as.Date(&quot;2021-03-21&quot;),as.Date(&quot;2021-04-21&quot;),&quot;day&quot;),
                      rad= sample(1:10,32, replace = T))
  
  &gt; head(data)
          date rad
  1 2021-03-21   1
  2 2021-03-22   5
  3 2021-03-23   1
  4 2021-03-24   9
  5 2021-03-25  10
  6 2021-03-26   4
  ...
  </code></pre>
  <p>I am currently learning to twist and manipulate big datasets and stumbled upon a case where my R knowledge/googling skills are just not helping anymore.</p>
  <p>I would like to learn two things:</p>
  <ol>
  <li><p>How to assign values to a variable for a certain period. Let's say for example, I want to give a value of 42 to all elements from the rad column for the time period between 2021-04-01 and 2021-04-05.</p>
  </li>
  <li><p>More importantly (and unrelatedly to 1. ) I would like to create a code that:</p>
  </li>
  </ol>
  <ul>
  <li>creates a new column based on the &quot;rad&quot; variable</li>
  <li>calculates the cumsum of &quot;rad&quot; for a certain time period (e.g. 2021-04-01 - 2021-04-05)</li>
  <li>Then takes the last value of the cumsum (the total sum of the cumulative summed period) and assigns it for a certain time period (e.g. 2021-04-06 - 2021-04-15)</li>
  <li>The new variable has the same values as the &quot;rad&quot; variable for rest of the dates to which no function is applied</li>
  </ul>
  <p>I simply do not know how to present you the desired output.</p>