我正在学习R,我想知道TRUE ~ "Other"在这种代码中做什么?
gapminder <- gapminder %>%
mutate(group = case_when(
.$region %in% west ~ "West" # assign the factor "West" for countries with region = west
.$region %in% c("Eastern Asia", "South-Eastern Asia") ~ "East Asia"
.$region %in% c("Carribbean", "Central America", "South America") ~ "Latin America"
.$continent == "Africa" & .$region != "Northern Africa" ~ "Sub-Saharan Africa",
TRUE ~ "Other"))
Case_when在mutate内部特别有用,当您想要创建一个依赖于现有
的复杂组合的新变量时https://dplyr.tidyverse.org/reference/case_when.html
TRUE指定不满足其他条件时的默认值。因此,在您的示例中,当区域不属于以下区域时:
西方- 亚洲东部亚洲东南部加勒比地区中美洲南美洲
或者当洲为"Africa",这个地区不是"北非"。我要特别注意这里你创建了一个名为区域的新变量但是你已经有了"北非"作为预定义区域,但不是在代码
中预定义的。那么其他任何东西都将被归类为"其他";