Session Ready
Exercise

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.

Instructions
100 XP
  • Count the number of "a" in each girl_names, store in number_as.
  • Count the number of "A" in each girl_names, store in number_As.
  • Create histograms, use the hist() function, of number_as and number_As. Why is number_As only zero or one?
  • Add together number_as and number_As to get total_as.
  • Subset girl_names to only those names where total_as > 4.