R语言 忽略NA时有条件地替换值

v64noz0r  于 2023-03-15  发布在  其他
关注(0)|答案(5)|浏览(169)

我有一个名为region的变量,其中包含世界的区域,变量Country包括国家名称。
有时Country具有NA值,但仍包括该区域。我只想将法国的区域从“Europe”更改为“France”,而不更改region变量中的任何其他内容。但是,当我运行以下代码时,对于Country中具有NA的所有观测,region都更改为NA

data <- data|>  
          mutate(region = case_when(
                            data$`Country` == "France" ~ "France",
                            data$`Country` != "France" ~ region)
                            )

我该如何解决这个问题?

oogrdqng

oogrdqng1#

在您的代码中,只有当Country != "France"TRUE时才会返回region,而当CountryNA时则不会返回region。您可以通过在条件中包含对NA s的测试来解决此问题:

library(dplyr)

data |>
  mutate(region = case_when(
    Country == "France" ~ "France",
    Country != "France" | is.na(Country) ~ region
  ))

或者使用ifelse()

data |>
  mutate(region = ifelse(
    Country == "France" & !is.na(Country),
    "France", 
    region
  ))

或者,在dplyr v1.1.0或更高版本中,可以使用case_when().default参数:

data |>
  mutate(region = case_when(
    Country == "France" ~ "France",
    .default = region
  ))

以下任一方法的结果:

# A tibble: 4 × 2
  Country     region
  <chr>       <chr> 
1 France      France
2 Germany     Europe
3 <NA>        Asia  
4 South Korea Asia
  • 示例数据:*
data <- tibble(
  Country = c("France", "Germany", NA, "South Korea"),
  region = c("Europe", "Europe", "Asia", "Asia")
)
cgyqldqp

cgyqldqp2#

我肯定有更干净的解决办法,但你可以试试:

data|>  
  mutate(region = case_when(
    Country == "France" ~ "France",
    Country != "France" ~ region,
    is.na(Country) ~ region)
  )
pgky5nke

pgky5nke3#

只需使用ifelse并检查Country中的NA值和不等于"France"

data |>  
  mutate(region = ifelse(is.na(Country) | Country != "France", region, Country)) -> data
wtzytmuj

wtzytmuj4#

data %>%
   mutate(region =case_match(Country,'France'~'France',.default = region))

# A tibble: 4 × 2
  Country     region
  <chr>       <chr> 
1 France      France
2 Germany     Europe
3 NA          Asia  
4 South Korea Asia 

data %>%
   mutate(region1 = coalesce(`is.na<-`(region, Country == 'France'), Country))

# A tibble: 4 × 3
  Country     region region1
  <chr>       <chr>  <chr>  
1 France      Europe France 
2 Germany     Europe Europe 
3 NA          Asia   Asia   
4 South Korea Asia   Asia
cbwuti44

cbwuti445#

尝试使用ifelse()而不是case_when()

data<- data %>%  
  mutate(region = ifelse(Country =="France","France",region)
  )

相关问题