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

Time:10-04

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)

CodePudding user response:

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

CodePudding user response:

df %>% 
   mutate(Var1 = coalesce(Var2, Var1))

   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
  • Related