 <p>The questions has been answered but I wanted to add a comment about why your first try gives an unexpected result. This is a good example of R's vector recycling.</p> <p>I'm guessing you got</p> <pre><code> year value 6 2008 4 13 2011 8 </code></pre> <p>Why has R done this? What happens is R recycles the vector <code>c("2008", "2009", "2010", "2011")</code> like the below. </p> <pre><code>year value compare 2006 3 2008 2007 4 2009 2007 3 2010 2008 5 2011 2008 4 2008 2008 4 2009 2009 5 2010 2009 9 2011 2010 2 2008 2010 8 2009 2011 3 2010 2011 8 2011 2011 7 2008 2012 3 2009 2013 4 2010 2012 6 2011 </code></pre> <p>Do you see what's about to happen? When you run </p> <pre><code>df&lt;-df[df$year == c("2008", "2009", "2010", "2011"),] </code></pre> <p>it will return the rows where the <code>year</code> column and the <code>compare</code> column are equal. You didn't get a warning because (by chance) your comparison vector was a divisor of the number of rows, so R thought it was doing the right thing. </p>