I am trying to code a new variable based on the values of three other variables. Specifically, if all of the variables are NA, I would like the new variable to take NA and if any of them are 1, it should take a 1, otherwise it should take a 0. However, I encounter an error using the following code:
frame <- data.frame(x = c(NA,NA,1), x2 = c(NA, NA, 0), x3 = c(NA,0,0))
frame
y <- ifelse(is.na(frame$x) == TRUE & is.na(frame$x2) == TRUE & is.na(frame$x3) == TRUE, NA, 0)
y2 <- ifelse(frame$x == 1 | frame$x2 == 1 | frame$x3 == 1, 1, y)
The second digit for y is correctly 0 but becomes NA when referenced in y2. Any idea on why this happens?