我正在尝试用R做一些代码分析,但遇到了一个问题。实际上,我使用的是“人口数据”,我想根据地区对国家进行排序(大洋洲、亚洲、欧洲......等等)。为此,我需要使用一个条件结构来检查一个国家是否在某个地区。尽管如此,我无法对多个地区(两个以上)执行此操作。
作为第一个解决方案,我尝试用if,else if,else来构造条件结构:
if(population_df$country %in% europe_countries) {
population_df$region <- "Europe"
}
else if(population_df$country %in% africa_countries) {
population_df$region <- "Africa"
}
else if(population_df$country %in% america_countries) {
population_df$region <- "America"
}
else if(population_df$country %in% oceania_countries) {
population_df$region <- "Oceania"
}
else if(population_df$country %in% asia_countries) {
population_df$region <- "Asia"
}
但它不起作用,因为控制台加载错误如下:
如果(% europe_countries中的population_df$country %){:条件的长度大于1
通过在网页上搜索,我理解了这个错误,因为我不能在if结构中使用向量(我不知道为什么,但这是事实)。
我用if_else结构尝试了第二种解决方案:
population_df$region <- if_else(population_df$country %in% europe_countries, "Europe", "")
population_df$region <- if_else(population_df$country %in% africa_countries, "Africa", "")
但正如所料,它不起作用,因为如果我有一个欧洲国家,我的第二个if_else将用“"替换“Europe”。
我没有其他办法让我的剧本运作起来,所以如果你们中的任何人有一个想法,那将是一种荣幸。
这里我的完整代码:
library(tidyverse)
library(ggplot2)
View(population)
spe_countries <- c("Nigeria", "Egypt", "Ethiopia", "France", "Germany", "Spain", "United States of America",
"Canada", "Mexico", "Australia", "New Zealand", "China", "Japan", "India")
europe_countries <- c("France", "Germany", "Spain")
africa_countries <- c("Nigeria", "Egypt", "Ethiopia")
america_countries <- c("United States of America","Canada", "Mexico")
oceania_countries <- c("Australia", "New Zealand")
asia_countries <- c("China", "Japan", "India")
population_df <- population %>%
filter(country %in% spe_countries)
View(population_df)
if(population_df$country %in% europe_countries) {
population_df$region <- "Europe"
}
else if(population_df$country %in% africa_countries) {
population_df$region <- "Africa"
}
else if(population_df$country %in% america_countries) {
population_df$region <- "America"
}
else if(population_df$country %in% oceania_countries) {
population_df$region <- "Oceania"
}
else if(population_df$country %in% asia_countries) {
population_df$region <- "Asia"
}
population_df$region <- if_else(population_df$country %in% europe_countries, "Europe", "")
population_df$region <- if_else(population_df$country %in% africa_countries, "Africa", "")
1条答案
按热度按时间yc0p9oo01#
包
countrycode
有一个名为codelist_panel的 Dataframe ,其中包含国家/地区的信息,包括它们各自所在的洲。通过
left_join
用codelist_panel(仅国家和大陆列)输入df,它会自动将大陆与正确的国家匹配。