title: Vectorization with precedent index
body: <p>Trying to vectorize in R, I often found myself blocked when coding those kind of 'for' loop:</p>
  
  <pre><code>  vector = 1:10
    cumulativeSum = vector[1]
    for (i in seq_along(vector)[-1]) {
      cumulativeSum[i] = cumulativeSum[i-1] + vector[i]
    }
  </code></pre>
  
  <p>The need of a precedent index that you cannot know in advance force me into using a third function or into rcpp to still avoid those cumbersome 'for' loops.
  In this exemple, we could use cumsum() to avoid this. I also use the diff() function often. </p>
  
  <p>Is there some other functions that are often usefull in those situations? Is there a general way of avoiding the issue.  </p>
  
  <hr>
  
  <p>First example:</p>
  
  <pre><code>c[i] =  ( a[i]*s[i] + ( (c[i-1]*w[i])/l )) / (
    (  ((w[i]-w[i-1])+w[i]) / l ) + o[i] )
  </code></pre>
  
  <p>Second example: 
  Obtaining an increasing vector</p>
  
  <pre><code>vector = c(1,2,3,4,5,3,9,10)
  for (i in rev(1:(length(vector )-1))){
    if (vector [i] &gt; vector [i+1]) vector [i] = vector [i+1]
  }
  # c(1,2,3,3,3,3,9,10)
  </code></pre>