title: Remove rows out of a specific year range, without using for-loop in R
body: <p>I am looking for a way to omit the rows which are not between two specific values, without using <code>for</code> loop. All rows in year column are between 1999 and 2002, however some of them do not include all years between these two dates. You can see the initial data as follows:</p>
  
  <pre><code>a &lt;- data.frame(year = c(2000:2002,1999:2002,1999:2002,1999:2001), 
                  id=c(4,6,2,1,3,5,7,4,2,0,-1,-3,4,3))
  
     year id
  1  2000  4
  2  2001  6
  3  2002  2
  4  1999  1
  5  2000  3
  6  2001  5
  7  2002  7
  8  1999  4
  9  2000  2
  10 2001  0
  11 2002 -1
  12 1999 -3
  13 2000  4
  14 2001  3
  </code></pre>
  
  <p>Processed dataset should only include consecutive rows between 1999:2002. The following <code>data.frame</code> is exactly what I need:</p>
  
  <pre><code>  year id
  1 1999  1
  2 2000  3
  3 2001  5
  4 2002  7
  5 1999  4
  6 2000  2
  7 2001  0
  8 2002 -1
  </code></pre>
  
  <p>When I execute the following <code>for</code> loop, I get previous <code>data.frame</code> without any problem:</p>
  
  <pre><code>for(i in 1:which(a$year == 2002)[length(which(a$year == 2002))]){
    if(a[i,1] == 1999 &amp; a[i+3,1] == 2002){
      b &lt;- a[i:(i+3),]
    }else{next}
  
    if(!exists("d")){
      d &lt;- b
    }else{
      d &lt;- rbind(d,b)
    }
  }
  </code></pre>
  
  <p>However, I have more than 1 million rows and I need to do this process without using <code>for</code> loop. Is there any faster way for that?</p>