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

没有正确的解决方案

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top