[R] NA values trimming

hadley wickham h.wickham at gmail.com
Mon Jul 6 10:18:02 CEST 2009


On Mon, Jul 6, 2009 at 12:12 AM, nyk<nick at nyk.ch> wrote:
>
> Thanks for your reply! This is what I was looking for!
> I'm using
> nas1 <- apply(data_matrix,1,function(x)sum(is.na(x))/nrow(data_matrix))
> nas2 <- apply(data_matrix,2,function(x)sum(is.na(x))/ncol(data_matrix))

You can simplify this a little:

perc_missing <- function(x) mean(is.na(x))

nas1 <- apply(data_matrix,1, perc_missing)
nas2 <- apply(data_matrix,2, perc_missing)

or if your matrix is really big the following should be faster:

nas1 <- rowMeans(is.na(data_matrix))
nas2 <- colMeans(is.na(data_matrix))

Hadley

-- 
http://had.co.nz/




More information about the R-help mailing list