加入國家欄位
ccode 欄中的國家代碼是所謂的 Correlates of War 代碼。這對分析並不理想,因為你會想用一眼看得懂的國家名稱。
你可以使用 countrycode 套件來轉換。例如:
library(countrycode)
# 將國家代碼 2 轉成國家名稱
> countrycode(2, "cown", "country.name")
[1] "United States"
# 一次轉換多個國家代碼
> countrycode(c(2, 20, 40), "cown", "country.name")
[1] "United States" "Canada" "Cuba"
本練習屬於課程
案例研究:R 中的探索性資料分析
練習說明
- 載入
countrycode套件。 - 將國家代碼 100 轉成對應的國家名稱。
- 在你的
mutate()敘述中加入新的country欄位,內容為國家名稱,並使用countrycode()從ccode欄位進行轉換。將結果存到votes_processed。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Load the countrycode package
# Convert country code 100
# Add a country column within the mutate: votes_processed
votes_processed <- votes %>%
filter(vote <= 3) %>%
mutate(year = session + 1945)