I have a simplified dataframe as follows:
test <- data.frame(
x = c(1,2,3,NA,NA,NA),
y = c(NA, NA, NA, 3, 2, NA),
a = c(NA, NA, NA, NA, NA, TRUE)
)
I want to test if when there’s NA in column x, there will always be a numeric value in column y AND when there’s a numeric value in column ‘x’, there will always be a NA in column y. How can I do that?
Thanks!
>Solution :
You can do:
(is.na(test$x) & is.numeric(test$y) & !(is.na(test$y))) |
(is.na(test$y) & is.numeric(test$x) & !(is.na(test$x)))
#[1] TRUE TRUE TRUE TRUE TRUE FALSE