Splitting the dataset
In a previous exercise, you have determined that the mean number of retweets by tweet is 3.3. In this exercise, we'll have a look at how many tweets are above this mean, and how many are below.
To do so, we will first create a mapper that tests if .x
is above 3.3
. We'll then prefill map_at()
, with .at
being "retweet_count"
, and .f
being first the mapper we've created, and in a second time the negation of this mapper.
Note that since this course was created, purrr
behavior changed, and in order to avoid an argument clash between .f
in partial()
and .f
in map_at()
, you must use the quasi-quotation equals operator, :=
, (sometimes known as the "walrus operator"). For the purpose of this exercise, all you need to know is that :=
works like =
, but lets partial()
know that the argument should be passed to map_at()
rather than being kept for itself.
Once these tools are created, we will use them on the non_rt
object, which is an extraction of the "original tweets" from the rstudioconf
dataset.
purrr
has been loaded for you.
This exercise is part of the course
Intermediate Functional Programming with purrr
Exercise instructions
Create
mean_above
, a mapper that tests if.x
is above3.3
.Prefill two version of
map_at()
: one with"retweet_count"
&mean_above
, and the other with"retweet_count"
& the negation ofmean_above
.Map these two prefilled functions on
non_rt
, and keep only the"retweet_count"
elements.Get the size of the two results.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create mean_above, a mapper that tests if .x is over 3.3
mean_above <- ___(~ ___)
# Prefil map_at() with "retweet_count", mean_above for above,
# and mean_above negation for below
above <- partial(___, .at = "retweet_count", .f := ___ )
below <- partial(___, .at = "retweet_count", .f := ___ )
# Map above() and below() on non_rt, keep the "retweet_count"
ab <- ___(non_rt, ___) %>% ___("retweet_count")
bl <- ___(non_rt, ___) %>% ___("retweet_count")
# Compare the size of both elements
___(ab)
___(bl)