'ifelse is giving me NAs when I want 0s

I am trying to create a variable using ifelse, and would like the column to be 0 when the condition is not true, but they are showing up as NA. Any tips?

df$z <- ifelse (df$x == 1 |
                df$y == 1, 1,
                ifelse (df$x == 2 |
                        df$y == 2 ,2, 0))

My understanding is that the above code should make everything that isn't 1 or 2 coded as 0, but I only get 1, 2 and NA. No zeros.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source