title: R: How to write a function to do summation of a particular column values and report the row numbers when it meet certain criteria?
body: <p>I am new to <code>R</code> and specially programming <code>function</code>.So, I would highly appreciate if you  please try to help to understand the concepts. </p>
  
  <p>I'd like to know how I can write a <code>function</code> to do the following acts:</p>
  
  <p>1- <code>add</code> the values of certain <code>variable</code>(column) <code>line by line (row by row)</code> and <br>
  2- then if the <code>sum</code> of the value would be greater than 100; <code>&gt;100</code><br>
  3- then, <code>stop</code> and report all of the rows <em>from begging till it meets the criteria</em> (total sum &lt;100) to stop?</p>
  
  <p>As an example :</p>
  
  <pre><code>DT &lt;- data.table(ID = c(1:21),Var1 = LETTERS[1:21], Var2 = seq(9, 19, .5))
  &gt; DT
      ID Var1 Var2
   1:  1    A  9.0
   2:  2    B  9.5
   3:  3    C 10.0
   4:  4    D 10.5
   5:  5    E 11.0
   6:  6    F 11.5
   7:  7    G 12.0
   8:  8    H 12.5
   9:  9    I 13.0
  10: 10    J 13.5
  11: 11    K 14.0
  12: 12    L 14.5
  13: 13    M 15.0
  14: 14    N 15.5
  15: 15    O 16.0
  16: 16    P 16.5
  17: 17    Q 17.0
  18: 18    R 17.5
  19: 19    S 18.0
  20: 20    T 18.5
  21: 21    U 19.0
      ID Var1 Var2
  </code></pre>
  
  <p>I'd like to <code>add</code> all of the value in <code>Var2</code> row by row until the <code>total sum</code> would be <code>&gt; 100</code> then it stops and report all the rows from beginning till it meet this criteria. <br></p>
  
  <p>I this case I mean, it add all of the values in <code>Var2</code> from <code>ID</code> <code>1:9 (9.0 +9.5+10+10.5+11+11.5+12+12.5+13)</code> which the total would be <code>99</code> and if add the next row (13.5) the values would be <code>112.5</code>.</p>
  
  <p>therefore the expected output would be :</p>
  
  <pre><code> ID Var1 Var2
   1:  1    A  9.0
   2:  2    B  9.5
   3:  3    C 10.0
   4:  4    D 10.5
   5:  5    E 11.0
   6:  6    F 11.5
   7:  7    G 12.0
   8:  8    H 12.5
   9:  9    I 13.0
  </code></pre>
  
  <p>I tried different things , but to me they were not useful at all, hence, I am not including them here .</p>