You did not specify that you need the R base, so I would gladly suggest using the str_count (string, pattern) function from the Hadley Wickham stringr package.
library(stringr) string <- "I do not like green eggs and ham!" pattern <- "(egs|ham)" str_count(string, pattern) [1] 1
stringr is really a great R. package. Complete of all kinds of string utility.
source share