title: R Programming: Using previously calculated row to update each row
body: <p>I have a very large time series and I need to create a different time series based on some arbitrary value in the beginning and change in the current time period. In the real dataset this change depends on other variables of the data frame, but for a purpose of MWE, I recreate it as follows:</p>
  
  <pre><code>initial_value &lt;- 100
  set.seed(123)
  library(data.table)
  df &lt;- as.data.table(data.frame(num = c(1:10),change = rnorm(10)))
  </code></pre>
  
  <p>The new variable <code>value</code> is defined as its own value in the previous period plus the <code>change</code> in the current period. The value in the first observation is determined by an arbitrarily chosen <code>initial_value</code>. If there were no restrictions on <code>value</code>, it could be created simply as</p>
  
  <pre><code>df &lt;- df[, value0 := initial_value + cumsum(change)]
  </code></pre>
  
  <p>This is very fast using <code>data.table</code>. However, unfortunately, <code>change</code> may also depend on the actual <code>value</code> in the previous period. Specifically, let's assume that whenever it reaches 102, the series needs to get to the <code>initial_value</code> in the next period and stay there for 3 periods. Thus, in the following data frame, I need to create the variable <code>value</code> while the code above produced <code>value0</code>:</p>
  
  <pre><code>    num      change    value0     value
   1:   1 -0.56047565  99.43952  99.43952
   2:   2 -0.23017749  99.20935  99.20935
   3:   3  1.55870831 100.76806 100.76806
   4:   4  0.07050839 100.83856 100.83856
   5:   5  0.12928774 100.96785 100.96785
   6:   6  1.71506499 102.68292 102.68292
   7:   7  0.46091621 103.14383 100.00000
   8:   8 -1.26506123 101.87877 100.00000
   9:   9 -0.68685285 101.19192 100.00000
  10:  10 -0.44566197 100.74626  99.55434
  </code></pre>
  
  <p>So far the only way I managed to produce this result is using a loop:</p>
  
  <pre><code>df$value &lt;- NA 
  df$value[1] &lt;- initial_value + df$change[1]
  for (i in 2:nrow(df)) {
    if (is.na(df$value[i])) {
      if (df$value[i-1] &lt; 102) {
        df$value[i] &lt;- df$value[i-1] + df$change[i]
      } else {
        df$value[i:(i+2)] &lt;- initial_value
      } 
    }
  }
  </code></pre>
  
  <p>However, looping over (dozens of) millions of observations is extremely slow. Is there a way to possibly vectorize it or simply run the process more efficiently?</p>