Exercise

Final challenges

You've mastered using stringr functions on their own, but by combining multiple operations together in sequence you can achieve quite complicated manipulations.

As the final exercise we want to expose you to the power of combining operations. You'll complete two tasks:

  1. You'll turn a vector of full names, like "Bruce Wayne", into abbreviated names like "B. Wayne". This requires combining str_split(), str_sub() and str_c().

  2. You'll compare how many boy names end in "ee" compared to girl names. This requires combining str_sub() with str_detect() along with the base function table().

Instructions 1/2

undefined XP
  • 1
    • Use str_split() to create a two column matrix with the first names in one column and the last names in the other. You'll be manipulating the first name before combining it back with the second name in the next few steps.
    • Create an abbreviation of the first name, abb_first, by extracting just the first character with str_sub().
    • Use str_c() to combine abb_first with ". " and the last name column to finish the task!
  • 2
    • Use str_sub() to extract the last two letters from all_names.
    • Create a logical, ends_in_ee that identifies whether the last_two_letters are ee, using str_detect().
    • Subset the rows of babynames_2014 using ends_in_ee and keep only the sex column. (Use the $ notation to subset the sex column.)
    • Apply table() to the result to complete the task!