partial string matching in R ,apart from using grep

162 views Asked by At

I need to do partial string matching R:

eg: str1<-"here we go"
    str2<-"here we go another way"

I need to do something like grep(str1,str2) ,i.e. the output should return whether str1 is in str2 or not. One way to do is grep but grep is running very slow. How could this be done in a faster way?

Thanks

1

There are 1 answers

0
agstudy On

Using grepl for example. Here a wrapper function:

str1 <- "here we go"
str2 <- "here we go another way"

`%instr%` <- 
  function(s1,s2) grepl(s1,s2) || grepl(s2,s1)

str1 %instr% str2
## [1] TRUE