Get startedGet started for free

Manipulating URLs

1. Manipulating URLs

Let's move to part 3 of our case study.

2. Creating mappers

Remember the as_mapper() function? It allows to quickly create a mapper, which are the one-sided formulas you can add as the dot f part of your map_*() calls. With as_mapper(), you're creating a reusable mapper, that is to say, that instead of being created on the fly inside the map_*() call, it can be reused several times in your code.

3. stringr::str_detect()

In the exercise, we'll be using a function from the stringr package to detect patterns in URLs. This function, called str_detect(), is a predicate that returns TRUE if the provided pattern is in the character vector, and FALSE otherwise. As you can see in the slide, the first element of the output is TRUE, as the pattern "life" has been detected in the first element of the lyrics object. The others are FALSE as there is no "life" in them.

4. Side note on logicals

When you are using predicates, you might want to know how many of the elements in the output are TRUE, and how many are FALSE. This can be done by calling the sum() function on the vector of logicals: when doing the sum of a logical vector, the TRUE values are converted to 1, and the FALSE values are converted to 0. This way, you can get the number of TRUE inside the vector.

5. Let's practice!

Now is the time to put this into practice.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.