title: find the first maximum occurence in vector
body: <p>Let's say I have a vector</p>
  
  <pre><code>x &lt;- c(300, 320, 342, 355, 399, 453, 500, 532, 2, 3, 4, 5, 100, 300, 500, 550, 2, 3)
  </code></pre>
  
  <p>As you can see it has some increasing values, then the values are decreasing, then increasing again and so on.</p>
  
  <p>The point where the first decreasing is spotted is in value 532 followed by value 2.So, at this point I want to add to 532 the value 2, so I have 534 now.Then add to 534 the value 3, then value 4 then value 5 and so on.</p>
  
  <p>So, I cumsum the values after the first maximum occurence which is value 532.</p>
  
  <p>So, my result would be:</p>
  
  <pre><code>300, 320, 342, 355, 399, 453, 500, 532, 534, 537, 541, 546, 646, 964, 1446, 1996, 1998 , 2001
  </code></pre>
  
  <p>I am trying: </p>
  
  <pre><code>MY_FUNC &lt;- function(Values)
  {
  
      Values &lt;- Values[!is.na(Values)]
  
      max_val = 0
      index = 0;
      for (i in 1:length(Values))
      {
          if (Values[i] &gt; max_val)
          {
              max_val = Values[i]
              index = i;
              #break when first occurence found
          }
      }
      new_vec &lt;- Values[index] + cumsum(Values[(index + 1):length(Values)])
      new_vec
  
  }
  
  x &lt;- c(300, 320, 342, 355, 399, 453, 500, 532, 2, 3, 4, 5, 100, 300, 500, 550, 2, 3)
  MY_FUNC(x)
  </code></pre>
  
  <p>but I don't know how to find the first maximum occurence and break of the loop. </p>