Search code examples
rdplyrforcats

Conditionally replace levels of factor variable 1 for a specific variable 2


In my example:

# Packages
library(dplyr)

# My dataset
FARM <- c(rep("LIBERTY2",4),rep("OLDOAK",4))
AGE <- c(8,9,10,10,8,9,10,10)
Y = c(0,0,0,0,1,1,1,1)
DS_F <- data.frame(FARM=FARM,AGE=AGE,Y=Y)
str(DS_F)
# 'data.frame': 8 obs. of  3 variables:
#  $ FARM: chr  "LIBERTY2" "LIBERTY2" "LIBERTY2" "LIBERTY2" ...
#  $ AGE : num  8 9 10 10 8 9 10 10
#  $ Y   : num  0 0 0 0 1 1 1 1

I'd like a conditional mutate in just only LIBERTY2 factor in variable FARM and use the rule in another variable: if AGE is equal to 10 than 3, if AGE is equal to 9 than 2, if AGE is equal to 8 than 1.

My final output should be:
DS_F2
#       FARM AGE Y
# 1 LIBERTY2   1 0
# 2 LIBERTY2   2 0
# 3 LIBERTY2   3 0
# 4 LIBERTY2   3 0
# 5   OLDOAK   8 1
# 6   OLDOAK   9 1
# 7   OLDOAK  10 1
# 8   OLDOAK  10 1

Please, help me.


Solution

  • You can surround a recode() into if_else().

    library(dplyr)
    
    DS_F %>%
      mutate(AGE.2 = if_else(FARM == "LIBERTY2",
                             recode(AGE, `10` = 3, `9` = 2, `8` = 1),
                             AGE))
    
    #       FARM AGE Y AGE.2
    # 1 LIBERTY2   8 0     1
    # 2 LIBERTY2   9 0     2
    # 3 LIBERTY2  10 0     3
    # 4 LIBERTY2  10 0     3
    # 5   OLDOAK   8 1     8
    # 6   OLDOAK   9 1     9
    # 7   OLDOAK  10 1    10
    # 8   OLDOAK  10 1    10