I want to create a new variable c. Step 1, if a=1 then c=1 (regardless of b); step 2, if a=2 or b=2, then c=2.
a <- c (2, 2, 2, NA, NA, NA, 1, 1, 1)
b <- c (1, 2, NA, 1, 2, NA, 1, 2, NA)
ab <- data.frame (a, b)
abc <- ab %>%
mutate (c = ifelse (a == 1, 1, NA)) %>%
mutate (c = ifelse (a == 2 | b == 2, 2, c))
abc
a b c
1 2 1 2
2 2 2 2
3 2 NA 2
4 NA 1 NA
5 NA 2 2
6 NA NA NA
7 1 1 1
8 1 2 2
9 1 NA NA
I expect c to be (2,2,2,NA,2,NA,1,2,1), but the actual out put is (2,2,2,NA,2,NA,1,2,NA). Why is the last cell changed to be NA (it should be 1)?