Creating lambda functions
Do you recall the three vectors visit_a
, visit_b
and visit_c
from the A/B test from the last exercise? They are still available in your workspace.
Remember that these vectors contain the hourly visit rate by day. Each element of these vectors corresponds to one design of the website, randomly served to the visitors. We are going to turn these vectors into a daily number of visits, but this time, we'll use a mapper.
Using a mapper allows you to write reusable code: you will potentially be asked to redo this task, so if you have an already existing mapper, you will be able to reuse this object, instead of copying and pasting the same code again and again.
This exercise is part of the course
Intermediate Functional Programming with purrr
Exercise instructions
- Get the daily number of visits by mapping an anonymous function on
visit_a
. - Make this code more concise by using a mapper.
- Create a reusable mapper object called
to_day
. - Call
to_day
on the three vectors (make three calls).
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Turn visit_a into daily number using an anonymous function
map(visit_a, ___(x) {
___
})
# Turn visit_a into daily number of visits by using a mapper
map(visit_a, ___)
# Create a mapper object called to_day
to_day <- as_mapper(___)
# Use it on the three vectors
map(visit_a, ___)
map(visit_b, ___)
map(visit_c, ___)