Detect matches (2)

  • str_detect() with summarize():
    • sum(str_detect(x, pattern)) returns the number of observations that match
    • mean(str_detect(x, pattern)) returns the proportion that match.
# compute and visualize the proportion of baby names that contain “x”
# broken down by year
babynames |> 
  group_by(year) |> 
  summarize(prop_x = mean(str_detect(name, "x"))) |> 
  ggplot(aes(x = year, y = prop_x)) + 
  geom_line()