开始使用免费开始使用

拆分数据集

在之前的练习中,您已经计算出每条推文的平均转推数为 3.3。这个练习中,我们将查看有多少推文高于该均值,以及有多少低于该均值。

为此,我们首先创建一个 mapper,用于测试 .x 是否大于 3.3。然后我们将预填充 map_at(),其中 .at"retweet_count".f 首先是我们创建的 mapper,接着是该 mapper 的取反。

请注意,自本课程创建以来,purrr 的行为发生了变化。为避免 partial() 中的 .fmap_at() 中的 .f 发生参数冲突,您必须使用类引用的等号运算符 :=(有时称为 "walrus operator")。在本练习中,您只需知道 := 的作用类似于 =,但它会让 partial() 知道该参数应传递给 map_at(),而不是被 partial() 自己保留。

准备好这些工具后,我们会将它们用于 non_rt 对象,它是从 rstudioconf 数据集中提取出来的「原创推文」。

已为您加载 purrr

本练习是课程的一部分

使用 purrr 的函数式编程进阶

查看课程

练习说明

  • 创建 mean_above,一个用于测试 .x 是否大于 3.3 的 mapper。

  • 预填充两版 map_at():一个使用 "retweet_count"mean_above,另一个使用 "retweet_count"mean_above 的取反。

  • 将这两个预填充函数映射到 non_rt,并仅保留 "retweet_count" 元素。

  • 获取两个结果的大小。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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)
编辑并运行代码