R语言 如果if_else不能解决问题,则条件长度大于1时出错

cetgtptt  于 2023-01-18  发布在  其他
关注(0)|答案(1)|浏览(236)

我正在尝试用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", "")
yc0p9oo0

yc0p9oo01#

countrycode有一个名为codelist_panel的 Dataframe ,其中包含国家/地区的信息,包括它们各自所在的洲。
通过left_join用codelist_panel(仅国家和大陆列)输入df,它会自动将大陆与正确的国家匹配。

library(tidyverse)

population %>%
  left_join(
    .,
    countrycode::codelist_panel %>%
      select(1, 5) %>%
      distinct(country = country.name.en, continent)
  )

Joining, by = "country"
# A tibble: 4,060 × 4
   country      year population continent
   <chr>       <int>      <int> <chr>    
 1 Afghanistan  1995   17586073 Asia     
 2 Afghanistan  1996   18415307 Asia     
 3 Afghanistan  1997   19021226 Asia     
 4 Afghanistan  1998   19496836 Asia     
 5 Afghanistan  1999   19987071 Asia     
 6 Afghanistan  2000   20595360 Asia     
 7 Afghanistan  2001   21347782 Asia     
 8 Afghanistan  2002   22202806 Asia     
 9 Afghanistan  2003   23116142 Asia     
10 Afghanistan  2004   24018682 Asia     
# … with 4,050 more rows
# ℹ Use `print(n = ...)` to see more rows

相关问题