title: R - sum a set number of rows from same column in a different data frame
body: <p>I have the following data frame:</p>
  <pre><code>    df &lt;- data.frame( year = c(1985,1986,1987,1988,1989,1990,
                         1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,
                         2001,2002,2003,2004,2005,2006,2007,2008,2009, 2010,
                         2011,2012, 2013,2014,2015,2016,2017,2018,2019,2020),
  
                       value = c(0,5,10,2,6,7,3,4,5,9,10,6,8,7,3,5,2,10,9,6,5,10,4,7,8,10,
                          4,6,8,9,2,3,7,6,2,1))
  </code></pre>
  <p>I want to create a second data frame (df2) that consists of 20 years intervals from the previous data frame, i.e.</p>
  <pre><code>df2 &lt;- data.frame(year=c(&quot;1985-2005&quot;, &quot;1986-2006&quot;,&quot;1987-2007&quot;, &quot;1988-2008&quot;,&quot;1989-2009&quot;,
                           &quot;1990-2010&quot;, &quot;1991-2011&quot;,&quot;1992-2002&quot;, &quot;1993-2003&quot;,&quot;1994-2004&quot;,
                           &quot;1995-2005&quot;, &quot;1996-2006&quot;,&quot;1997-2007&quot;, &quot;1998-2008&quot;, &quot;1999-2009&quot;,
                           &quot;2000-2020&quot;))
  </code></pre>
  <p>Now the <code>value</code> for <code>df2</code> should be the sum of <code>value</code> on <code>df</code> for 20 years intervals
  (i.e., for year &quot;1985-2005&quot; in <code>df2</code>, the value is the sum of values from 1985 until 2005 in <code>df</code> - Excel snapshot attached with final values)</p>
  <p><a href="https://i.stack.imgur.com/7gYwU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/7gYwU.png" alt="enter image description here" /></a></p>
  <p>How can I perform this calculation? Also any possible automation to define the <code>year</code> interval in <code>df2</code> without having to type it?</p>