I have two 'sets' of binary variables in a R data frame (X and Y). The X variables contain the information if the value in Y is valid (1) or not (0). I would like to use the X variables as a mask and replace all invalid cells of the Y variables with NA, where the corresponding X variable is 0.
Simplified example:
input <- tibble(X1 = c(1,1,0,1), X2 = c(1,1,1,0), X3 = c(0,0,0,1), Y1 = c(1,0,1,1), Y2 = c(1,1,0,0), Y3 = c(0,0,0,0))
output <- tibble(Z1 = c(1,0,NA,1), Z2 = c(1,1,0,NA), Z3 = c(NA,NA,NA,0))
data.frameortibble? If not using anarray:A <- array(unlist(input), c(4,3,2)); replace(A[,,2], A[,,1] !=1 , NA)