使用countrycode
包中的countrycode()
函数将国家转换为大洲。请确保安装并加载countrycode
包。以下是代码示例:
# 安装并加载 countrycode 包
install.packages("countrycode")
library(countrycode)
# 创建一个示例数据框
data <- data.frame(
Country = c("China", "India", "United States", "Brazil", "Russia"),
Value = c(10, 20, 30, 40, 50)
)
# 将国家转换为大洲(使用countrycode函数)
data$Continent <- countrycode(data$Country, "country.name", "continent")
# 输出转换后的数据
print(data)
Country
列包含了一些国家名称,Value
列是示例数据。countrycode()
函数将Country
列中的国家名称转换为对应的大洲,并将结果存储在Continent
列中。