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

Chuck Cleland ccleland at optonline.net
Fri Feb 9 10:40:43 CET 2007


Richard M. Heiberger wrote:
> 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)

  Possibly simpler (does not require a new function definition and seems
highly intuitive) might be something like this:

tmp.dropna <- tmp[,colSums(is.na(tmp)) < 2]

tmp.dropna
   C  D
a 13 19
b 14 20
c 15 21
d 16 22
e 17 23
f NA 24

> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

-- 
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list