sweatcrepe
sweatcrepe

Reputation: 23

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

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!

Upvotes: 0

Views: 396

Answers (3)

Alan Gómez
Alan Gómez

Reputation: 378

Also this:

df1$Continent <- ifelse(df1$Country=="Fiji","Oceania",ifelse(df1$Country=="USA","Americas",df1$Continent))

Is a simple replacement in Base R

Upvotes: 0

akrun
akrun

Reputation: 887721

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

Upvotes: 3

Allan Cameron
Allan Cameron

Reputation: 174468

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

Upvotes: 3

Related Questions