title: Adding multiple rows conditonally in R data frame (loop)
body: <p>I have trouble with adding multiple rows into a data frame under a certain condition.</p>
  
  <p>My data set looks something like this:</p>
  
  <pre><code>ID TIME DV
  1   0.5 1
  1   2   2.3
  1   30  0.02
  1   35  0.01
  2   0.4 0.9
  2   2   2.8
  2   30  0.01 
  </code></pre>
  
  <p>Now I want to add rows for several IDs under the conditon TIME >30.</p>
  
  <p>The output should look like this</p>
  
  <pre><code>ID TIME DV
  1   0.5 1
  1   2   2.3
  1   30  0.02
  1   35  0.01
  1   100 0
  2   0.4 0.9
  2   2   2.8
  2   30  0.01 
  </code></pre>
  
  <p>I have tried the following code:</p>
  
  <p><code>cond  &lt;- data2[data2$TIME &gt; 30,]
  cond_1 &lt;- rownames(cond) 
  for(i in cond_1){data2 &lt;- rbind(data2[1:i,],newrow,data2[-(1:i),])}</code></p>
  
  <p>which works for the first insertion, but since the row numbers change with every addition of a new row it does not work for the rest.</p>
  
  <p>Unfortunately I have no idea how to work around this problem and the data set is to large to do it manually...</p>
  
  <p>Thank you very much in advance!</p>
  
  <p>Kind regards</p>