Counting matches
Another stringr function that takes a vector of strings and a pattern is str_count(). str_count() answers the question "How many times does the pattern occur in each string?". It always returns an integer vector of the same length as that of the input vector.
If you count the occurrences of "pepper" in your pizzas, you'll find no occurrences in the first, and one each in the second and third,
pizzas <- c("cheese", "pepperoni",
"sausage and green peppers")
str_count(pizzas, pattern = fixed("pepper"))
Perhaps a little more interesting is to count how many "e"s occur in each order
str_count(pizzas, pattern = fixed("e"))
You'll use str_count() to find some names with lots of repeated letters.
Este exercício faz parte do curso
String Manipulation with stringr in R
Instruções do exercício
- Count the number of
"a"in eachgirl_names, store innumber_as. - Count the number of
"A"in eachgirl_names, store innumber_As. - Create histograms, use the
hist()function, ofnumber_asandnumber_As. Why isnumber_Asonly zero or one? - Add together
number_asandnumber_Asto gettotal_as. - Subset
girl_namesto only those names wheretotal_as > 4.
Exercício interativo prático
Experimente este exercício completando este código de exemplo.
# Count occurrences of "a" in girl_names
number_as <- ___
# Count occurrences of "A" in girl_names
number_As <- ___
# Histograms of number_as and number_As
___
___
# Find total "a" + "A"
total_as <- ___
# girl_names with more than 4 a's
___