title: Rolling calculations with lag and ifelse
body: <p>I'm trying to calculate the cumulative sum of a column in my data frame, but I only want it to be done based on a condition.</p>
  <p>I am very new to R and trying to convert this spss code below in R to achieve the dataframe below with a new column called cumulative based on whether the lca is the same as the row above and if it is then add health net cost to the cumulative cost above.</p>
  <pre><code>spss code:
  IF LAG(LCA) NE LCA cumulative=health_net_cost.
  IF LAG(LCA)=LCA cumulative=LAG(cumulative)+health_net_cost.
  EXECUTE.
  </code></pre>
  <p>Dataframe in R</p>
  <pre><code>Lca health_net_cost   cummulative
  10  100         100     
  10  200         300
  10  300         600
  28  400         1000
  28  100         1100
  8   100         1200
  8   200         1400
  8   300         1700
  </code></pre>