Sean G Sean G - 4 months ago 12
R Question

r - Filter rows that contain a string from a vector

I'm looking for a function that takes a dataframe column, checks if it contains text from a vector of strings, and filters it upon match (including a partial text match).

For example, take the following data frame:

animal |count
aardvark |8
cat |2
catfish |6
dog |12
dolphin |3
penguin |38
prairie dog|59
zebra |17


and the following vector

c("cat", "dog")


I'd like to run through the 'animal' column, checking if the value fully or partially matches by one of the strings in the vector, and filter out the ones that aren't. The resulting data frame would be:

animal |count
cat |2
catfish |6
dog |12
prairie dog|59


Thank you!

Sean

Answer

We can use grep

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

Or using dplyr

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