'Intrincate variable generation with conditionals against multiple factor variables in R

I'm triying to generate a new variable using multiple conditionals that evaluate against factor variables.

So, let's say I got this factor variables data.frame

x<-c("1", "2", "1","NA", "1", "2", "NA", "1", "2", "2", "NA" )

y<-c("1","NA", "2", "1", "1", "NA", "2", "1", "2", "1", "1" )

z<-c("1", "2", "3", "4", "1", "2", "3", "4", "1", "2", "3")

w<- c("01", "02", "03", "04","05", "06", "07", "01", "02", "03", "04")


df<-data.frame(x,y,z,w)

df$x<-as.factor(df$x)
df$y<-as.factor(df$y)
df$z<-as.factor(df$z)
df$w<-as.factor(df$w)

str(df) 

So I need to get a new v colum on my dataframe which takes values between 1, 0 or NA with the following conditionals:

Takes value 1 if: x = "1", y = "1", z = "1" or "2", w = "01" to "06"

Takes value 0 if it doesn't meet at least one of the conditionals.

Takes value NA if any of x, y, z, or w is NA.

Had tried using a pipe %>% along mutate and case_when but have been unable to make it work.

So my desired result would be a new column v in df which would look like this:

[1] 1 NA 0 NA 1 NA NA 0 0 0 NA



Sources

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

Source: Stack Overflow

Solution Source