1. Using purrr in your workflow
In our last chapter, we will go over how to solve more complex problems with purrr, using the skills learned in the previous chapters.
2. Checking for names
Let's review from previous chapters. When working with a list, often our first step is to check if a list has names with the names() function. Our dataset, Star Wars films, does not have any names.
So we can use the set_names() function, along with map_chr() to specify the element in the list we want to use for the names. First, we start with map_chr(), which has two arguments, the first argument is our sw_films list, the second is the name of the subelement that contains the names we want to use, in this case, title. Second we put the map_chr() function inside the set_names() function.
Now we can see that each element of the list is named with the name of the film it contains data about.
3. Setting names while asking questions
We can also set names as a part of a larger piped workflow. We will continue using data about the Star Wars films in this example. We want to be able to sort our sw_films list by episode id and use the title of the movies to set the names. So we start with map_chr(), which we use to pull out the element we want to sort by, episode_id. Then we pipe that into set_names(), where we use a second map_chr() function to pull out the element with the titles, which is simply called title. Then we pipe that result into sort(). It returns a character vector that is sorted by the episode id, which represent the order of the timeline for each Star Wars film, that is the time the film is set in, not the release dates for each film.
4. Let's purrr-actice!
Now let's try some examples.