Walter Walter - 1 year ago 77
R Question

Subset a dataframe by multiple factor levels

How can I avoid using a loop to subset a dataframe based on multiple factor levels?

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".

Working example:

#sample data
Value<-c(1, 2, 3, 4, 1, 2, 3, 4)
data<-data.frame(cbind(Code, Value))

selected<-c("A","B") #want rows that contain A and B

#Begin subsetting

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.

Answer Source

You can use %in%

  data[data$Code %in% selected,]
  Code Value
1    A     1
2    B     2
7    A     3
8    A     4
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download