I used grepl to check whether a string contains any of the patterns from a set of patterns (I used '|' to separate the patterns). Reverse search didn't help. How to identify the set of patterns that match?
Additional information: This can be solved by writing a loop, but it is very time consuming as my set has > 100,000 strings. Can it be optimized?
Eg: Let the string be a <- "Hello"
pattern <- c("ll", "lo", "hl")
pattern1 <- paste(pattern, collapse="|") # "ll|lo|hl"
grepl(a, pattern=pattern1) # returns TRUE
grepl(pattern, pattern=a) # returns FALSE 'n' times - n is 3 here
You are looking for str_detect
from package stringr
:
library(stringr)
str_detect(a, pattern)
#[1] TRUE TRUE FALSE
In case you have multiple strings like a = c('hello','hola','plouf')
you can do:
lapply(a, function(u) pattern[str_detect(u, pattern)])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With