R - Filter strings containing a string from a vector

I am looking for a function that takes a dataframe column, checks to see if it contains text from a row vector, and filters it after matching (including partial text matching).

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 next vector

 c("cat", "dog") 

I would like to run through the β€œanimal” column, checking whether this value fully or partially matches one of the lines in the vector, and filter out those that are not. Resulting data frame:

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

Thanks!

Sean

+5
source share
2 answers

We can use grep

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

Or using dplyr

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

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

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

I personally find that using dplyr and stringr easier to read after a few months while viewing the code.

+7
source

All Articles