<- c("apple", "banana", "pear", "kiwi") fruit library(stringr)str_count(fruit, pattern = "a")
Output:
[1] 1 3 1 0
str_count()
counts the number of occurrences of a specific pattern in a string. In the following example, we count the number of letter “a” in each of the fruit names.
<- c("apple", "banana", "pear", "kiwi") fruit library(stringr)str_count(fruit, pattern = "a")
Output:
[1] 1 3 1 0
Here we count the letter of “a”, “b”, “p”, and “p”, respectively, in each of the four fruit names. Note that the length of the pattern is the same as the input vector fruit
.
str_count(fruit, pattern = c("p", "a", "e", "i"))
Output:
[1] 2 3 1 2