Dplyr Mutate With Conditional Values

[Solved] Dplyr Mutate With Conditional Values | Perl - Code Explorer | yomemimo.com
Question : dplyr mutate with conditional values

Answered by : yan-zhang

myfile %>% mutate(V5 = (V1 == 1 & V2 != 4) + 2 * (V2 == 4 & V3 != 1))
or
myfile %>% mutate(V5 = ifelse(V1 == 1 & V2 != 4, 1, ifelse(V2 == 4 & V3 != 1, 2, 0))) V1 V2 V3 V4
1 1 2 3 5
2 2 4 4 1
3 1 4 1 1
4 4 5 1 3
5 5 5 5 4
... V1 V2 V3 V4 V5
1 1 2 3 5 1
2 2 4 4 1 2
3 1 4 1 1 0
4 4 5 1 3 0
5 5 5 5 4 0

Source : | Last Update : Tue, 24 May 22

Answers related to dplyr mutate with conditional values

Code Explorer Popular Question For Perl