5

I've got a string:

string <- "I do not like green eggs and ham!"

and a pattern

pattern <- "(egs|ham)"

I want to know how many times pattern matches string with fuzzy matching (agrep).

gregexpr will do this for normal matching - I just want to know if there's a corresponding garegexpr in R or a way to emulate it without being too performance-heavy.

(aregexec will only return the index for the first match, "eggs", and skip "ham").

mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194

1 Answers1

-1

You didn't specify that you need base R, so I'll happily suggest using the str_count(string, pattern) function from the "stringr" package by Hadley Wickham.

library(stringr)
string <- "I do not like green eggs and ham!"
pattern <- "(egs|ham)"
str_count(string, pattern)
[1] 1

stringr really is a great R package. Full of all sorts of string usefulness.

ClintWeathers
  • 576
  • 7
  • 22
  • That's great, but it isn't fuzzy matching. I'm after a similar function but using `agrep` instead of `grep` (I can already emulate `str_count` using `gregexpr`) – mathematical.coffee Apr 10 '13 at 01:49