문제

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