添加国家列
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)