設定名稱
在很多情境下,替清單設定名稱能讓操作變得容易許多;也會讓程式碼更容易閱讀,這在幾週或幾個月後回頭檢視程式碼時特別重要。
在這裡,你要使用 gh_repos 和 gh_users 資料集,並用兩種不同方式設定它們的名稱。這兩種方法會得到相同的結果:一個具有命名元素的清單。
本練習屬於課程
使用 purrr 的函式式程式設計基礎
練習說明
- 使用元素「name」為
gh_users設定名稱,並使用會輸出字元向量的map_*()函式。 - 探索
gh_repos的結構,找出儲存擁有者資訊的位置。 - 使用
set_names()和map_*()函式,以版本庫擁有者的登入名稱為依據,為新的清單gh_repos_named設定名稱。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# 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(___, .)