1. Learn
  2. /
  3. Courses
  4. /
  5. Intermediate Functional Programming with purrr

Connected

Exercise

Playing with URLs

In the last exercise, we've extracted the raw number of tweets which contain the pattern "github". So far, this is just a raw number: 347, and you're still not able to determine which proportion of the total number of URLs this 347 represents.

In this exercise, we'll build a function called str_prop_detected(), which takes a character vector and a pattern, and returns the proportion of elements which contain this pattern. This function could be used on other datasets to detect the proportion of pattern matches in a character vector.

Here, we'll combine some purrr functions with the str_detect() function from stringr. These two packages have been loaded for you, and the rstudioconf dataset is still available in your workspace.

Instructions 1/2

undefined XP
    1
    2
  • Complete the definition of str_prop_detected(). Use str_detect() to detect which values of string contain pattern, and calculate the mean of the occurrences.
  • Combine flatten() and compact() as flatten_and_compact().