简体   繁体   中英

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM