Pitfall: case sensitive regex

Possible fixes:

  • Add the upper case vowels to the character class: str_count(name, "[aeiouAEIOU]").
  • Ignore case: str_count(name, regex("[aeiou]", ignore_case = TRUE)).
  • Use str_to_lower() to convert the names to lower case.
babynames |> 
  count(name) |> 
  mutate(
    name = str_to_lower(name),
    vowels = str_count(name, "[aeiou]"),
    consonants = str_count(name, "[^aeiou]")
  )
## # A tibble: 97,310 × 4
##    name          n vowels consonants
##    <chr>     <int>  <int>      <int>
##  1 aaban        10      3          2
##  2 aabha         5      3          2
##  3 aabid         2      3          2
##  4 aabir         1      3          2
##  5 aabriella     5      5          4
##  6 aada          1      3          1
##  7 aadam        26      3          2
##  8 aadan        11      3          2
##  9 aadarsh      17      3          4
## 10 aaden        18      3          2
## # ℹ 97,300 more rows