[R] removing NA values from data frame & identification function

Prof Brian D Ripley ripley at stats.ox.ac.uk
Sun Apr 23 13:07:15 CEST 2000


On Wed, 19 Apr 2000, Ben Bolker wrote:

> On Wed, 19 Apr 2000, Faheem Mitha wrote:
> 
> > Dear people,
> > 
> > 1) I have a data frame with named columns.For concreteness, let us say
> > that I created a data frame from the vectors fee, fi fo, fum by
> > 
> > giant.df <- cbind(fee, fi, fo, fum)
> > 
> > Now, some of the entries in fee, fi fo fum are NAs. I want to remove any
> > row which contains a NA, thus creating a new, smaller data frame, with the
> > same column names. This seems like something people would need to do all
> > the time, but I have been unable to think of a clean way to do it.
> 
>   perhaps
> 
> clean.df <- giant.df[apply(giant.df,1,function(z)all(!is.na(z))),]
> 
>  would do what you wanted?
> 
>  Or, slightly more tersely,
> 
>             giant.df[!(apply(is.na(giant.df),1,any)),]

How about na.omit(giant.df)?  That is the intended use of na.action
functions. The help file says

     `na.omit' returns the data frame with incomplete cases removed.


-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list