Get startedGet started for free

Working with unnamed lists

1. Working with unnamed lists

Let's dive into more complex tasks with purrr. This is where it gets FUN!

2. But first, pipes

Since purrr is a part of the set of packages called the tidyverse, it works very well with pipes. A pipe, that is the percent sign, greater than symbol, percent sign, takes the output from the function in front of it and makes it the input for the next function. Pipes are powerful because they prevent us from needing intermediate objects. We'll be using pipes in the coming chapters since they can be used inside and outside of purrr functions. As you can see, both of these statements give the same result but the first, using pipes, is much cleaner.

3. Does my list have names?

Let's walk through an example where pipes can be used. If we don't know if a list has named elements, we can use the names() function in two different ways. First, we can put the list, in this case, our amphibian survey_data from the previous chapter, in the names() function. Or we can start with the name of the list, survey data, and then pipe it into the names function. The output is the same.

4. No names? Set some!

If a list doesn't have names, we can set them, which is really helpful for indexing, subsetting, and generally working with a list. Especially one that might grow or change over time where numeric indexing ultimately won't be useful. Here we are going to use the Star Wars Films dataset from the repurrrsive package, by Jenny Bryan. This dataset is a list of lists. Where each element of sw_films is data about each of the Star Wars films, and each element is another list, where the subelements have different kinds of information. Here we are showing the first four subelements from that list. We need to extract names from the list using the map_chr() function. The first argument in map_chr() is the name of our list, sw_films, and our second argument is the name of the element where the names are stored, in this case, title. We then put the map_chr() function inside the set_names() function. Then we can check that setting names worked by calling the names() function on sw_films.

5. Pipes within map()

In addition to using pipes to connect together parts of our workflow outside of map() functions, we can also use pipes within map(). Here we are working with a list called waterfowl_data. Waterfowl tend to move in large flocks, so our counts of them migrating over different sites are not normally distributed. So we are going to sum up the counts of bird migration over different sites. Each site is an element in the list. Then we are going to log transform the data, to make comparisons of very large and very small counts easier. We can do this all within one map() function by putting pipes inside the function itself. Our first argument in map() will still be the name of the list. Our second argument is our entire workflow and starts with a tilde, then a dot x, and then we pipe it into sum(), and then into log(). This will return a list, where each element is the logged sum of the numbers in the previous list's values.

6. Let's purrr-actice!

Now it's your turn.