1. Using mappers to clean up your data
Now that we have seen the basics of mappers let's use them to clean your data!
2. Setting the name of your objects
First, let's start with set_names() a function from purrr that, as its name suggests, sets the names of a list.
For example, here, the visits2016 vector is not named, so we'll add the month abbreviation. We're doing this because it's easier to work on a named list. For example, if you want to filter elements out of the list, you'll know which elements you have extracted, as they are named.
Note that the second element of set_names() must have the same length as the first element.
3. Setting names - an example
As you can see, we can use set_names() inside a map() function, by building a mapper that will be applied to each element of the dot x argument.
What we are doing here is taking every element of the all_visits vector and applying set_names() to each of these elements, with the month abbreviation as a parameter.
4. keep()
keep() is a convenient function that helps to make a selection inside a list, by only keeping the elements that satisfy a condition.
This condition can be a standard test like is.numeric(), or a mapper, as you can see on this slide.
For example, if we need to know which months had more than 30,000 visits during the year 2016, we can use keep() to extract the months that satisfy the condition described in the mapper.
5. discard()
discard() is the counterpart of keep(): it removes all the elements that satisfy a condition.
As you can see, while on the last slide I have kept the elements that showed a sum above 30000, I'm removing them when using discard().
6. keep(), discard(), and map()
Use keep() and discard() along with map() to clean your list.
Here, for example, we want to keep only the factor columns of each data.frame. The first line of code creates a list that contains the first six row of the iris and airquality datasets. The second line of code keeps the factor from these data frames.
There is no factor column in the airquality dataframe. Maybe you would have expected keep() to returns an error if there is nothing to keep. That's not the case: keep() returns an empty object in that case.
7. Let's practice!
So, are you a keeper? Let's find out in the exercises!