Clean up your data with keep
Since the beginning of this course, we have been using the results of a weeklong A/B test.
We have put these results in a list called all_visits
. This list contains visit_a
, visit_b
, and visit_c
. These vectors are unnamed. They all contain seven numbers, one for each day of the week.
The first question we want to ask is: which days reached more than 100 visits an hour on average? We will use the keep()
function. But the answer would not be readable with an unnamed vector: you would have the numbers, but you would not know to which day these numbers correspond.
The good news is: you can use the set_names()
function to solve this issue. This is what we'll do in this chapter: first, use keep()
on unnamed vectors, then on named ones.
This exercise is part of the course
Intermediate Functional Programming with purrr
Exercise instructions
- Create a mapper that will test if
.x
is more than 100. You'll use it twice. - Combining this mapper with
keep()
, and map it on the unnamed listall_visit
. As the result is unnamed, you don't know which days you have kept. - Name each vector by combining
map()
and theset_names()
functions, using the vector of names we have provided. - Map the previously created mapper on the newly named list. As you can see, it's more readable now!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create a mapper that test if .x is more than 100
is_more_than_hundred <- ___(~ .x > 100)
# Use this mapper with keep() on the all_visits object
map(all_visits, ~ ___(.x, is_more_than_hundred))
# Use the day vector to set names to all_list
day <- c("mon", "tue", "wed", "thu", "fri", "sat", "sun")
full_visits_named <- map(all_visits, ~ ___(.x, ___))
# Use this mapper with keep()
map(full_visits_named, ~ ___(.x, ___))