filtering multiple rows in one column in r

You can filter multiple rows in one column using the %in% operator with the filter() function from the dplyr package. Here's an example:

Suppose you have a data frame df with a column col and you want to filter all rows where col equals a list of values list_vals. You can do:

main.r
library(dplyr)

list_vals <- c("value1", "value2", "value3")

df_filtered <- df %>%
  filter(col %in% list_vals)
113 chars
7 lines

This will return a new data frame df_filtered with only the rows of df where col equals "value1", "value2", or "value3".

related categories

gistlibby LogSnag