I have a data:
test_df <- data.frame(x1 = c("a", "b", "c", NA, NA),
x2 = sample(1:5),
x3 = c(T, NA, F, T, NA),
x4 = c(NA, NA, 1, 2, 3),
stringsAsFactors = F)
colset1 <- c("x1", "x2", "x3")
colset2 <- c("x2", "x3", "x4")
data frame and vectors containing variable names.
How to check (best in dplyr way), if any row of columns provided in a vector (colset) contains any NAs?
Expected answer for colset1 is TRUE FALSE TRUE FALSE FALSE, and for colset2 is FALSE FALSE TRUE TRUE FALSE (best if can be mutated as a new logical variable, doesn't matter).
The alternative question will be: how to count NAs in that columns?
Expected answer for colset1 is 0 1 0 1 2, and for colset2 is 1 2 0 0 1
I was trying with mutating ...ifelse(length(sum(is.na(vars(colset1)))) == 0) but something was still missing, it didn't work and I got lost in own code :)
Thanks!