Home > Enterprise >  Replace NA values based on the value of another column in R
Replace NA values based on the value of another column in R

Time:09-17

I have a table like this:

Country Continent
Fiji na
Fiji na
USA Americas
France Europe
Italy Europe
U.S.A na
France Europe

How can I replace the NAs in Fiji and U.S.A to "Oceania" and "Americas" respectively using dplyr?

Thanks!

CodePudding user response:

Using case_when:

library(dplyr)

df1 %>%
  mutate(Continent = case_when(
             Country == 'USA' ~ 'Americas',
             Country == 'Fiji' ~ 'Oceania',
             TRUE ~ Continent)))
#>   Country Continent
#> 1    Fiji   Oceania
#> 2    Fiji   Oceania
#> 3     USA  Americas
#> 4  France    Europe
#> 5   Italy    Europe
#> 6     USA  Americas
#> 7  France    Europe

Data taken from question in reproducible format

df1 <- structure(list(Country = c("Fiji", "Fiji", "USA", "France", "Italy", 
"USA", "France"), Continent = c(NA, NA, "Americas", "Europe", 
"Europe", NA, "Europe")), row.names = c(NA, -7L), class = "data.frame")  


df1
#>   Country Continent
#> 1    Fiji      <NA>
#> 2    Fiji      <NA>
#> 3     USA  Americas
#> 4  France    Europe
#> 5   Italy    Europe
#> 6     USA      <NA>
#> 7  France    Europe

Created on 2022-09-16 with reprex v2.0.2

CodePudding user response:

We could use

library(dplyr)
keydat <- data.frame(Country = c("Fiji", "USA"), 
   Continent = c("Oceania", "Americas"))
df1 %>% 
  left_join(keydat, by = "Country") %>% 
  transmute(Country, Continent = coalesce(Continent.x, Continent.y))

-output

  Country Continent
1    Fiji   Oceania
2    Fiji   Oceania
3     USA  Americas
4  France    Europe
5   Italy    Europe
6     USA  Americas
7  France    Europe

Or with rows_update

rows_update(df1, keydat)
Country Continent
1    Fiji   Oceania
2    Fiji   Oceania
3     USA  Americas
4  France    Europe
5   Italy    Europe
6     USA  Americas
7  France    Europe
  • Related