Suppose I have the following dataframe:
x <- c(1, 1, 2, 3, 4, 5)
y <- c(1, 1, 1, 3, 4, 5)
z <- c(NA, 1, 1, 3, 4, NA)
to get:
x y z
1 1 NA
1 1 1
2 1 1
3 3 3
4 4 4
5 4 NA
and I wanted to get a conditional statement such that if all of the non-NA x, y, and z values are equal to 1, then it would be flagged as 1, how would I go about writing this script?
For instance, what I want is the following:
x y z flag1
1 1 NA 1
1 1 1 1
2 1 1 0
3 3 3 0
4 4 4 0
5 4 NA 0
Additionally, I would also want to flag if any of the variables contained a 4, ignoring NA, so that I can get:
x y z flag1 flag2
1 1 NA 1 0
1 1 1 1 0
2 1 1 0 0
3 3 3 0 0
4 4 4 0 1
5 4 NA 0 1