stringr (version 0.5)

str_count: Count the number of matches in a string.

Description

Count the number of matches in a string.

Usage

str_count(string, pattern)

Arguments

string
input character vector
pattern
pattern to look for, as defined by a POSIX regular expression. See the ``Extended Regular Expressions'' section of regex for details.

Value

  • integer vector

Details

Vectorised over string and pattern, shorter is recycled to same length as longest.

See Also

regexpr which this function wraps

str_locate/str_locate_all to locate position of matches

Examples

Run this code
fruit <- c("apple", "banana", "pear", "pineapple")
str_count(fruit, "a")
str_count(fruit, "p")
str_count(fruit, "e")
str_count(fruit, c("a", "b", "p", "p"))

Run the code above in your browser using DataLab