擷取多個州的資料
在前一個練習中,你學會了如何用 rbind_tigris() 函式合併資料集。不過,若你需要超過 2 個州的資料,這個流程就會變得繁瑣。在本練習中,你會學到如何使用 tidyverse 的 map() 函式為多個州產生資料集清單,並再用 rbind_tigris() 合併這些資料集。
本練習屬於課程
在 R 中分析美國人口普查資料
練習說明
- 建立一個州代碼的向量,包含 Maine、New Hampshire、Vermont 與 Massachusetts,命名為
new_england。 - 使用 tidyverse 的
map()函式來迭代該州代碼向量。 - 在
map()所使用的函式中,設定區域變數x,以請求每個州的 tract 資料。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Generate a vector of state codes and assign to new_england
___ <- c("ME", "NH", "VT", "MA")
# Iterate through the states and request tract data for state
ne_tracts <- ___(new_england, function(x) {
___(state = ___, cb = TRUE)
}) %>%
rbind_tigris()
plot(ne_tracts$geometry)