Home > OS >  How to overwrite values of column 1 with column 2 except for column 2's NA values?
How to overwrite values of column 1 with column 2 except for column 2's NA values?

Time:10-09

I have two columns for Race, Race 1 and Race 2. Race 2 has some corrections for Race 1, but it is most NA values.

Here is an example:

Race1    Race2

White    American Indian
White    American Indian
White    Black
Black    NA
Black    NA
White    NA
White    NA

The results I want are as folloiws:

Race    

American Indian
American Indian
Black
Black    
Black    
White    
White    

CodePudding user response:

You can do the following:

library(dplyr)

df %>%
  summarise(Race = coalesce(!!!select(rev(.), everything())))

             Race
1 American_Indian
2 American_Indian
3           Black
4           Black
5           Black
6           White
7           White

CodePudding user response:

Perhaps you can try replace along with is.na

data.frame(Race = with(df,replace(Race2, is.na(Race2), Race1[is.na(Race2)])))

CodePudding user response:

A variation on the theme using ifelse():

df1$Race = with(df1, ifelse(is.na(Race2), Race1, Race2))
df1
#> # A tibble: 7 × 3
#>   Race1 Race2           Race           
#>   <chr> <chr>           <chr>          
#> 1 White American Indian American Indian
#> 2 White American Indian American Indian
#> 3 White Black           Black          
#> 4 Black <NA>            Black          
#> 5 Black <NA>            Black          
#> 6 White <NA>            White          
#> 7 White <NA>            White

Created on 2022-10-08 with reprex v2.0.2

data

df1 <- structure(list(Race1 = c("White", "White", "White", "Black", 
                                "Black", "White", "White"), Race2 = c("American Indian", "American Indian", 
                                                                      "Black", NA, NA, NA, NA)), class = c("tbl_df", "tbl", "data.frame"
                                                                      ), row.names = c(NA, -7L))

CodePudding user response:

The simplest way is with dplyr::coalesce:

  df %>% transmute(Race = coalesce(Race2,Race1))

Resulting in

Race           
  <chr>          
1 American Indian
2 American Indian
3 Black          
4 Black          
5 Black          
6 White          
7 White  
  • Related