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.
CodePudding user response:
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
CodePudding user response:
Is this what you are looking for?
DS_F <- DS_F %>%
mutate(FARM = as.factor(FARM),
RULE = case_when(AGE == 10 ~ 3,
AGE == 9 ~ 2,
AGE == 8 ~ 1))
which produces
> DS_F %>%
mutate(FARM = as.factor(FARM),
RULE = case_when(AGE == 10 ~ 3,
AGE == 9 ~ 2,
AGE == 8 ~ 1))
FARM AGE Y RULE
1 LIBERTY2 8 0 1
2 LIBERTY2 9 0 2
3 LIBERTY2 10 0 3
4 LIBERTY2 10 0 3
5 OLDOAK 8 1 1
6 OLDOAK 9 1 2
7 OLDOAK 10 1 3
8 OLDOAK 10 1 3
with the following datatypes
> str(DS_F)
'data.frame': 8 obs. of 4 variables:
$ FARM: Factor w/ 2 levels "LIBERTY2","OLDOAK": 1 1 1 1 2 2 2 2
$ AGE : num 8 9 10 10 8 9 10 10
$ Y : num 0 0 0 0 1 1 1 1
$ RULE: num 1 2 3 3 1 2 3 3
CodePudding user response:
Using data.table
library(data.table)
setDT(DS_F)[FARM == "LIBERTY2", AGE := AGE - min(AGE) 1]
-output
> DS_F
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