1. The many flavors of map()
If you remember from the previous lessons, the map() function outputs a list. Sometimes we want our data in another class, which is where the other flavors of map() come in, giving us many different kinds of outputs.
2. Non-list outputs
In the last lesson, we saw that map() outputs a list. Sometimes this can be quite useful, but if we think about the kinds of problems we solve with data every day we may want a different kind of output, like a vector! This is where the other flavors of map() come in. Here is the example from the previous lesson, where the map() function gives us the number of rows in each dataframe from the survey_data list.
3. purrr::map_variants
Pulling out a vector of numbers from a list can be useful for a number of things, including as an input for our next task, or to create a new column in a dataframe. Here we are using another map() function, map underscore dbl to pull out the numbers that represent the number of rows in each dataframe as a vector, instead of a list. The regular map() function gives us a list, while map_dbl() outputs a vector of numbers as the result.
4. map_lgl()
A vector of TRUE and FALSE values can be valuable for checking to make sure an object contains what we think it does, or as an input to something else. Here we are interested in checking whether each element of our survey_data list has 14 rows, so we use the map underscore lgl function to get a logical vector as output.
5. map_chr()
A vector of characters can be valuable as a way of pulling information out of a list so it can be used for other things, often as an input for our next tasks. Here we want to pull out the names of the amphibians surveyed as a character vector with the site names. We do this with map_chr().
6. Example time!
Here we want to create a vector that contains the number of rows in each element of the survey_data list and make it a column in a new dataframe. First, we create our new dataframe, we'll call it survey_rows. This new dataframe has two columns, one called names, which contains the names of each element from the survey_data list. The second column is called rows and is empty for now.
Next, we use map_dbl() to create a vector of numbers. Our first argument is the survey_data list, our second argument is the tilde symbol followed by the nrow() function. Inside nrow() we put dot x, to represent where the element of the survey_data list should be placed. We then assign map_dbl() to the rows column in the new dataframe that we created.
Now we can use these names and rows to determine if these elements contain the number of rows expected, which is a vital part of the quality assurance and quality control process.
Unlike our example from the previous video, here we have a nice vector that easily works as a part of a dataframe column, whereas before the output was a list of values, which can be more challenging to work with.
7. Let's purrr-actice!
Now it's your turn.