设置名称
为列表元素设置名称在很多场景下都能显著简化操作;代码也更易读,尤其在几周或几个月后回头审阅时更重要。
本练习将使用 gh_repos 和 gh_users 两个数据集,并分别用两种方式为它们设置名称。这两种方法的结果相同:得到一个具名元素的列表。
本练习是课程的一部分
使用 purrr 掌握函数式编程基础
练习说明
- 使用
"name"元素为gh_users设置名称,并使用会返回字符向量的map_*()函数。 - 查看
gh_repos的结构,确定仓库所有者的信息存放位置。 - 基于仓库所有者的登录名,为新列表
gh_repos_named设置名称,使用set_names()和map_*()函数。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Name gh_users with the names of the users
gh_users_named <- ___ %>%
set_names(map___(___, ___))
# Check gh_repos structure
str(___)
# Name gh_repos with the names of the repo owner
gh_repos_named <- ___ %>%
map_chr(~ .[[___]]$___$___) %>%
set_names(___, .)