You could use stringi::stri_detect_fixed()
. It is vectorized over both str
and pattern
.
DT[stringi::stri_detect_fixed(category, industry)]
# category industry
# 1: administration admin
# 2: trucking truck
# 3: administration admin
# 4: trucking truck
# 5: nurse practitioner nurse
Alternatively, stringr::str_detect()
can be used. It is also vectorized over both its arguments.
library(stringr)
DT[str_detect(category, fixed(industry))]
Or a base R option is to run grepl()
through mapply()
DT[mapply(grepl, industry, category, fixed = TRUE)]
Or you could get the same result with Vectorize(grepl)
.
DT[Vectorize(grepl)(industry, category, fixed = TRUE)]
All of these produce the same result.
Data:
DT <- structure(list(category = c("administration", "nurse practitioner",
"trucking", "administration", "warehousing", "warehousing", "trucking",
"nurse practitioner", "nurse practitioner"), industry = c("admin",
"truck", "truck", "admin", "nurse", "admin", "truck", "nurse",
"truck")), .Names = c("category", "industry"), class = "data.frame", row.names = c(NA,
-9L))
setDT(DT)