title: Translating Conditional Sum from FORTRAN to R
body: <p>I am translating FORTRAN code to R and am getting stuck with a conditional sum. </p>
  
  <p>Some context to my code:
  I am looking at mass accumulation on a surface for each hour and rain cleaning the surface. If the rain is under a threshold of 1.5 cm then the mass accumulation over the hours is summed, but if the rain is above the 1.5 cm threshold then the mass accumulation sum is cleaned and reset to zero. For the following hours the process repeats, sum mass accumulation until rain is above 1.5, then the mass accumulation is set back to zero. </p>
  
  <p>So in FORTRAN I am going through an array row by row (using a DO LOOP) and calculating the total mass accumulation for that hour, "mtot". I sum this mtot value into "msum" if the corresponding rain experienced in that hour is less then 1.5 cm. If the rain is above 1.5 the msum and mtot are set to zero, since now the surface experienced no mass accumulation over that hour and the previous mass was cleaned away. I then move onto the next iteration and re-calculate mtot for that hour and repeat the same logic. So I am storing the i+1 hour's mtot over the i hour's mtot. </p>
  
  <p>Here is my FORTRAN code:</p>
  
  <pre><code>DO i = 1, 8760
      ...
      mtot = ...
  
  
      IF (rain_hourly(i) &gt;= 1.5) THEN             ! did it rain, was the rain enough to clean the surface, threshold to clean is 1.5 cm an hour
          msum = 0.0
          mtot = 0.0
      ELSE                                        ! sum mass on surface if it didnt rain enough
          msum = msum + mtot
      END IF
  
      ...
  END DO
  </code></pre>
  
  <p>Where I need help is translating my IF block into R. In R I calculate mtot for each hour at once and have a array of mtots for each hour. I think I am looking for a conditional if to sum the values up until the last zero seen in the hourly mtot array. I am new to R and am not to sure on how to approach it. I can think of many different ways to do it in FORTRAN but like I said I am new to R and don't quite grasp how R thinks basically.</p>
  
  <p>I have created a table to help better explain what I am trying to do. The hourly rain column contains the amount of rain experienced in an hour in cm's, recall that the threshold to clean the surface is 1.5 cm. The hourly mtot column is the amount of mass accumulated each hour. Please note that if the hourly rain column meets the threshold of 1.5 the hourly mtot for that hour will be overridden with a zero. Finally, the summed mtot column is the sum of the same row of  mtot along with the past mtot if the rain threshold has not been met. Please note that past mtots included in the sum are only back until the last time the rain threshold was met.</p>
  
  <p><code>**Hourly Rain**            **Hourly mtot**         **Summed mtot**
          1                         3                        3
          1                         1                        4
          1                         2                        6
          1.5                  0 -&gt; 1                        0
          1                         2                        2
          1.5                  0 -&gt; 1                        0
          1                         3                        3
          1                         1                        4
          1                         1                        5</code></p>