tidycensus 中的整齊與寬格式資料
tidycensus 的函式預設會回傳「整齊資料框」(tidy data frame),其中每一列代表一個唯一的單位—變數組合。不過,在某些視覺化或分析方法中,讓每個 Census 變數各自成為一個欄位會更方便。為了達成這點,你可以在呼叫 get_acs() 或 get_decennial() 時設定 output = "wide",這會把估計值/數值與誤差範圍放到各自的欄位中。
本練習屬於課程
在 R 中分析美國人口普查資料
練習說明
- 透過設定
output = "wide",以「寬格式」取得 Oregon 各郡的 ACS 家庭收入中位數與年齡中位數資料。 - 檢查寬格式資料框的前幾列。
- 使用
plot()函式,繪製家庭收入中位數與年齡中位數的散佈圖。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Return county data in wide format
or_wide <- get_acs(geography = ___,
___ = "OR",
variables = c(hhincome = "B19013_001",
medage = "B01002_001"),
output = ___)
# Compare output to the tidy format from previous exercises
head(___)
# Create a scatterplot
___(or_wide$hhincomeE, or_wide$medageE)