Search code examples
rconditional-statementstidyversecategorical-datarecode

Recoding categorical variable based on the value of another categorical variable in r data frame


I want to be able to recode Var1 to have the same value of Var2 (if different). Please note that I want to keep the value of Var1 if Var2 is NA. Any help would be appreciated. I do not mind have Var3 with the same outcome.

Var1 <- c("A", "A", "D", "B","C", "A", "C","C", "A", "C")
Var2 <- c("A", "A", "A", "C","C", NA, NA,"C", "A", "C")
df <- data.frame(Var1, Var2)

Desired data frame should be

Var1 <- c("A", "A", "A", "C","C", "A", "C","C", "A", "C")
Var2 <- c("A", "A", "A", "C","C", NA, NA,"C", "A", "C")
df <- data.frame(Var1, Var2)

Solution

  • A simple if_else statement works here:

    library(dplyr)
    
    df %>%
      mutate(Var1 = if_else(is.na(Var2), Var1, Var2))
    
    #    Var1 Var2
    # 1     A    A
    # 2     A    A
    # 3     A    A
    # 4     C    C
    # 5     C    C
    # 6     A <NA>
    # 7     C <NA>
    # 8     C    C
    # 9     A    A
    # 10    C    C