开始使用免费开始使用

添加国家列

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)
编辑并运行代码