I'm trying to count the number of NA in multiple columns of my data. Here is a reproducible sample.
structure(list(V2QE38A = c(1, 0, 1, 0, 1, 1, 1, 0, 1, 0), V2QE38B = c(0,
0, 0, 0, 0, 1, 0, 0, 0, 0), V2QE38C = c(1, 1, 0, 3, 2, 0, 0,
3, 1, 1), V2QE38D = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)), row.names = c(NA,
10L), class = "data.frame")
I tried two methods: First one:
dt %>% select(starts_with("V2QE38")) %>% colSums(is.na(.))
And this gives me some results (in short, I have NAs in some columns) Then I tried another one:
colSums(is.na(dt[,c("V2QE38A", "V2QE38B", "V2QE38C", "V2QE38D")]))
And I found no NA in any of these columns.
I think the second result is correct. But I'm just wondering what did I do wrong to get the first result? Thank you!