title: More Efficient Way To Do A Conditional Running Total In R
body: <p>As this is my first time asking a question on SO, I apologize in advance for any improper formatting.</p>
  
  <p>I am very new to R and am trying to create a function that will return the row value of a data frame column once a running total in another column has met or exceeded a given value (the row that the running sum begins in is also an argument).</p>
  
  <p>For example, given the following data frame, if given a starting parameter of x=3 and stop parameter of y=17, the function should return 5 (the x value of the row where the sum of y >= 17).</p>
  
  <pre><code>X   Y
  1   5
  2   10
  3   5
  4   10
  5   5
  6   10
  7   5
  8   10
  </code></pre>
  
  <p>The function as I've currently written it returns the correct answer, but I have to believe there is a much more 'R-ish' way to accomplish this, instead of using loops and incrementing temporary variables, and would like to learn the right way, rather than form bad habits that I will have to correct later.</p>
  
  <p>A very simplified version of the function:</p>
  
  <pre><code>myFunction&lt;-function(DataFrame,StartRow,Total){
      df&lt;-DataFrame[DataFrame[[1]] &gt;= StartRow,]
      i&lt;-0
      j&lt;-0
  
      while (j &lt; Total) {
          i&lt;-i+1
          j&lt;-sum(df[[2]][1:i])
      }
  
      x&lt;-df[[1]][i]
      return(x)
  }
  </code></pre>