1. Learn
  2. /
  3. Courses
  4. /
  5. String Manipulation with stringr in R

Exercise

Extracting age and gender from accident narratives

Recall from the video, you want to parse out age and gender from accident narratives. For example, this narrative

19YOM-SHOULDER STRAIN-WAS TACKLED WHILE PLAYING FOOTBALL W/ FRIENDS 

describes male of age 19, and this one

TRIPPED OVER CAT AND LANDED ON HARDWOOD FLOOR. LACERATION ELBOW, LEFT. 33 YOF*

a female of age 33.

You are generally looking for a pattern with a number, something to indicate the units, e.g. YO or YR for years old, or MO for months old, and a character that identifies the gender.

In this exercise you'll build up a pattern to pull out the part of the narrative that has the age and gender information. Then, in the next exercise you'll parse out the age and gender into separate variables.

Instructions 1/4

undefined XP
  • 1

    Create an age pattern that matches one or two digits. Test your pattern using str_view().

  • 2
    • Create a unit pattern that matches an optional space, then one of YO, YR or MO.
    • Check your pattern so far by using str_view() with the pattern age %R% unit.
  • 3
    • Create a gender pattern that matches an optional space then M or F.
    • Check your pattern so far by using str_view() with the pattern age %R% unit %R% gender.
  • 4

    Extract the age-unit-gender piece from each narrative using str_extract().