title: Subset a dataframe by multiple factor levels
body: <p>How can I avoid using a loop to subset a dataframe based on multiple factor levels?  </p>
  
  <p>In the following example my desired output is a dataframe.  The dataframe should contain the rows of the original dataframe where the value in "Code" equals one of the values in "selected".</p>
  
  <p>Working example:  </p>
  
  <pre><code>#sample data
  Code&lt;-c("A","B","C","D","C","D","A","A")
  Value&lt;-c(1, 2, 3, 4, 1, 2, 3, 4)
  data&lt;-data.frame(cbind(Code, Value))
  
  selected&lt;-c("A","B") #want rows that contain A and B
  
  #Begin subsetting
  result&lt;-data[which(data$Code==selected[1]),]
  s1&lt;-2
  while(s1&lt;length(selected)+1)
  {
    result&lt;-rbind(result,data[which(data$Code==selected[s1]),])
    s1&lt;-s1+1
  }
  </code></pre>
  
  <p>This is a toy example of a much larger dataset, so "selected" may contain a great number of elements and the data a great number of rows.  Therefore I would like to avoid the loop.  </p>