r - check if every column is na
Data
set.seed(1)
data <- c(LETTERS, NA)
df <- data.frame(Flag_A = sample(data), Flag_B = sample(data),
C = sample(data), D = sample(data), Flag_E = sample(data))
df <- rbind(NA, df)
Code
Identifying all NAs per row:
> df$All_NA <- apply(df[, grep("Flag", names(df))], 1, function(x) all(is.na(x)))
> head(df)
Flag_A Flag_B C D Flag_E All_NA
1 <NA> <NA> <NA> <NA> <NA> TRUE
2 H K B T Y FALSE
3 J W C K P FALSE
4 O I H I <NA> FALSE
5 V L M S R FALSE
6 E N P E I FALSE
Identifying at least one NA per row:
> df$Any_NA <- apply(df[, grep("Flag", names(df))], 1, function(x) anyNA(x))
> head(df)
Flag_A Flag_B C D Flag_E Any_NA
1 <NA> <NA> <NA> <NA> <NA> TRUE
2 H K B T Y FALSE
3 J W C K P FALSE
4 O I H I <NA> TRUE
5 V L M S R FALSE
6 E N P E I FALSE
I'm not sure what the grep
part is supposed to do, but here's a simpler way to accomplish what you want:
apply(ItemStats_2014[, 2:10], MARGIN = 1, FUN = function(x) all(is.na(x)))
Replace 2:10
with whatever columns you want to check.
Amendment: If you want to detect which columns contain the word "Flag" rather than hard coding their indices -- which would be better anyway! -- I like the package stringr
for working with text. You could do this to select your columns:
library(stringr)
MyCols <- which(str_detect(names(ItemStats_2014), "Flag"))
Now, replace 2:10
with MyCols
in the apply(...
code above.
I think that you are trying to test if a row (not a column) contains at least one NA.
Here a dataset
x = c(1:10, NA)
df = data.frame(A = sample(x), B = sample(x), C = sample(x))
And here a loop that test that with anyNA
df$Any_na = apply(df[,2:3], 1, anyNA)
df
A B C Any_na
1 NA 8 9 FALSE
2 5 9 NA TRUE
3 9 3 10 FALSE
4 7 5 1 FALSE
5 4 2 3 FALSE
6 10 4 6 FALSE
7 3 1 2 FALSE
8 6 6 5 FALSE
9 1 10 7 FALSE
10 2 NA 8 TRUE
11 8 7 4 FALSE