Get startedGet started for free

Hunting for matches

1. Hunting for matches

The most powerful set of tools in stringr are those that do something with strings and a pattern. It might be finding strings that contain a pattern, splitting strings into pieces based on a pattern, or replacing parts of a string that match a pattern.

2. stringr functions that look for matches

All of the stringr functions you'll see in the rest of this chapter take a pattern argument. To start off we'll look at the three stringr functions: str_detect, str_subset, and str_count. All three look for the pattern in the input strings, but they differ in the kind of output that is returned.

3. Finding matches

Let's start with str_detect. str_detect takes a vector of strings and a pattern and returns a logical vector that is TRUE for each string that contained the pattern and FALSE otherwise. Here is a vector of strings, each describes the toppings on a pizza. Let's look for the pizzas that contain the string "pepper". less than !-- Force output to line up with input strings to make correspondence clear - greater than The result is FALSE TRUE TRUE, cheese doesn't contain the string pepper, but both pepperoni and sausage and green peppers do. The pattern argument can be a regular expression, something you'll learn about in the next chapter. For now you want the pattern to always be taken literally, so you need to tell stringr to do that by wrapping the string you are looking for, in the function fixed. In this case it doesn't make a difference, but it can, especially if you are looking for patterns that contain punctuation like dots, dashes, brackets and slashes.

4. Finding matches

You use str_subset and str_count in exactly the same way, except str_subset returns only those strings that contained the pattern, and str_count returns the number of times the pattern occurred in each string. You'll try these functions out,

5. Let's practice!

by looking for names in babynames that contain specific patterns.