[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)
x[,!apply(x,2,function(y)any(is.na(y))]
or
x[,!apply(is.na(x),2,any)]
(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
x[!sapply(is.na(x),any)]
*does not* work.
x[complete.cases(t(is.na(x)))]
or t(na.omit(t(X)))
both do, if your data frame is all numeric.
Ben
More information about the R-help
mailing list