title: Iterating through all rows in R, removing those that fit criteria
body: <p>R data frame. It has about a dozen columns and 150 or so rows. I want to iterate through each row and remove it, under these two conditions</p>
  
  <ol>
  <li>It's value in column 8 is undefined</li>
  <li>The value for the row ABOVE it, in column 8 IS defined.</li>
  </ol>
  
  <p>My code looks like this, but it keeps crashing. It's gotta be a dumb mistake, but I can't figure it out.</p>
  
  <pre><code>for (i in 2:nrow(newfile)){
      if (is.na(newfile[i,8]) &amp;&amp; !is.na(newfile[(i-1),8]){ 
      newfile&lt;-newfile[-i,]
      }
  }   
  </code></pre>
  
  <p>Obviously in this example, newfile is my dataframe.</p>
  
  <p>The error I get</p>
  
  <blockquote>
    <p>Error in <code>[.data.frame</code>(newfile, -i, ) : object 'i' not found</p>
  </blockquote>
  
  <p>Problem solved, but some test data if you guys wanted to muck around:</p>
  
  <pre><code>23  L8  29141078    744319  27165443
  24  L8  27165443    NA  NA
  25  L8  28357836    8293    25116398
  26  L8  25116398    NA  NA
  27  L8  28357836    21600   25116398
  28  L8  25116398    NA  NA
  29  L8  40929564    NA  NA
  30  L8  40929564    NA  NA
  31  L8  41917264    33234   39446503
  32  L8  39446503    NA  NA
  33  L8  41917264    33981   39446503
  34  L8  39446503    NA  NA
  </code></pre>
  
  <p>Obviously a little modified here, so now you are comparing column 4 with the one above it (or you can use column 5, either way)</p>