Search/Filter with grepl with multiple patterns

Goal: Filter a dataframe, that contains parts of strings, that are in another dataframe.

# dataframe that contains strings
d1 <- c("halloauch", "hieronymus", "grüßdich", "hello", "hi", "hallo")
d1 <- data.frame(d1)
# dataframe that contains patterns
d2 <- c("hi", "hallo")
d2 <- data.frame(d2)

grepl can’t search through a dataframe. It needs a vector with the or-operator

convert patterns to vector

d2 <- as.vector(as.matrix(d2))

combine patterns to a single element

d2 <- paste(d2, collapse=“|”)

check, if dataframe contains patterns and filter these elements:

library(dplyr) d %>% filter(grepl(d2, d1$name))