r - Filter rows that contain a string from a vector

For large datasets the following base R approach can do the job 15x faster than accepted answer. At least that was my experience.

The code generates a new dataframe to store the subsets of rows that match a given value (animal).

#Create placeholder data frame
new_df <- df[0, ]

#Create vector of unique values
animals <- unique(df$animal)

#Run the loop
for (i in 1:length(animals)){
    temp <- df[df$animal==animals[i], ] 
    new_df <- rbind(new_df,temp)
}

Using dplyr, you can try the following, assuming your table is df:

library(dplyr)
library(stringr)
animalList <- c("cat", "dog")
filter(df, str_detect(animal, paste(animalList, collapse="|")))

I personally find the use of dplyr and stringr to be easier to read months later when reviewing my code.


We can use grep

df1[grep(paste(v1, collapse="|"), df1$animal),]

Or using dplyr

df1 %>%
    filter(grepl(paste(v1, collapse="|"), animal))

Tags:

R

Dplyr