[R] removing NA from a data frame

Ben Bolker bolker at ufl.edu
Sat Mar 18 01:05:52 CET 2006

Haifeng Xie <xieh <at> wmin.ac.uk> writes:

> If I understand it correctly, something like this should do what you want
> x[!apply(x, 1, function(y) any(is.na(y)), ]
> where x is the dataframe in question.
> Hope that helps.
> Kevin

   I believe he wants to remove *columns* with NAs, not rows
(if he wanted to remove rows then complete.cases(x) would work)




 (I wasn't sure one could apply() on columns of a data frame --
I'm always a little certain about the matrix <-> data.frame
mapping -- but I tried it and you can.  Now that I think
about it, I don't know why I thought you couldn't.  apply()
on rows would be more likely to be problematic.)

  is.na() turns a data frame into a matrix, so


*does not* work.


or t(na.omit(t(X)))

both do, if your data frame is all numeric.

More information about the R-help mailing list