title: In R, check multiple values multiple times, sum if conditions are met, keep track of position, export data, and repeat
body: <p>Here is a sampling of my data, which is in a matrix:</p>
  
  <pre><code>BLOCK  RUNTIME
  101    50
  101    20
  101    -25
  101    -40
  101    35
  101    45
  202    25
  202    -10
  202    -35
  202    40
  202    50
  202    30
  202    -20
  202    15
  .
  .
  .
  n
  </code></pre>
  
  <p>The desired output of my yet-to-be code is this:</p>
  
  <pre><code>BLOCK  TIME_CHUNKS
  101    70
  101    -65
  101    80
  202    25
  202    -45
  202    120
  202    -20
  202    15
  </code></pre>
  
  <p>What I want the code to do to get the output: for as long as subsequent rows' BLOCK values are equal to the current row's BLOCK value, AND for as long as subsequent rows' RUNTIME values are the same sign, sum them, and populate a new row in a table with the BLOCK value and the summed value.  Then proceed where you left off.  In the example data, the first two rows (50, 20) are summed and written to a table.  Then the sign of RUNTIME switches and is constant for two rows (-25, -40), so these values are summed.  Then the sign of RUNTIME switches again and is positive for three rows (35, 45, 25), but the last row of these three has a different BLOCK number, so only the first two of these three rows are summed and written to the table.  Proceed until the end of the matrix is reached.</p>
  
  <p>It's easy enough for me to make the conditional statements, but I don't know how to "keep track" of where I am in the matrix to restart the summing process.  I'm not that well-versed in for or while loops to even make an educated guess at how they could be used here.  I made a pathetic attempt at writing a function, but didn't get anywhere.</p>
  
  <p>Need to automate this process...I've got about 10,000 rows of data, which can be dynamically produced based on a set of scenario variables.  Each set of variables will produce a table with different values for RUNTIME, and I've got a lot of scenarios to run.</p>
  
  <p>Much appreciation for any help.</p>