Frage

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").

Keine korrekte Lösung

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top