[R] How to count the number of NAs in each column of a df?

Richard M. Heiberger rmh at temple.edu
Fri Feb 9 04:16:59 CET 2007


drop.col.kna <- function(mydf, k)
 mydf[sapply(mydf, function(x) sum(is.na(x))) < k]


tmp <- data.frame(matrix(1:24, 6,4, dimnames=list(letters[1:6], LETTERS[1:4])))
tmp[1:3,1] <- NA
tmp[2:5,2] <- NA
tmp[6,3] <- NA

drop.col.kna(tmp, 0)
drop.col.kna(tmp, 1)
drop.col.kna(tmp, 2)
drop.col.kna(tmp, 3)
drop.col.kna(tmp, 4)
drop.col.kna(tmp, 5)
drop.col.kna(tmp, 6)



More information about the R-help mailing list