title: R: How to sum values from rows only if the key value is the same and also if the key duplicated in consecutive rows?
body: <p>I have the following data table of this structure:</p>
  
  <pre><code>+-------------------+
  | id  | key | value |
  +-----+-----+-------+
  | 1   | A   | 1000  |
  | 1   | A   | 2000  |
  | 1   | B   | 2001  |
  | 1   | A   | 2002  |
  | 1   | A   | 2004  |
  | 2   | B   | 2002  |
  | 2   | C   | 2002  |
  +-------------------+
  </code></pre>
  
  <p>My objective is to sum the values by id and key but instead of just grouping by id and key, I only want to sum the values if the pair of id and key are the same for consecutive rows. </p>
  
  <p>The result should be:</p>
  
  <pre><code>+-------------------+
  | id  | key | value |
  +-----+-----+-------+
  | 1   | A   | 3000  |
  | 1   | B   | 2001  |
  | 1   | A   | 4006  |
  | 2   | B   | 2002  |
  | 2   | C   | 2002  |
  +-------------------+
  </code></pre>
  
  <p>Is there anyway to achieve this result?</p>