[R] how to skip NA columns ?

Prof Brian D Ripley ripley at stats.ox.ac.uk
Thu Jun 20 14:13:50 CEST 2002


Columns of what?  A data frame or a matrix?

For a data frame DF

hasna <- sapply(DF, function(x) any(is.na(x)))
DF <- DF[!hasna]

and `hasna' tells which you removed.  Use apply and matrix indexing for a
matrix.

On Thu, 20 Jun 2002, Czerminski, Ryszard wrote:

> R-helpers!
>
> na.omit() can be used to remove rows with NA's
> but how can I remove columns ? and remember, which columns have been removed
> ?
>
> I guess I can do t(na.omit(t(o))) as shown below, but this probably creates
> a lot of overhead and I do not know which columns
> have been removed.

It only works for matrices, too.

> Yours,
>
> R
>
> > o
>      [,1] [,2] [,3]
> [1,]    1   NA    7
> [2,]    2   NA    8
> [3,]    3   NA    9
> > t(na.omit(t(o)))
>      [,1] [,2]
> [1,]    1    7
> [2,]    2    8
> [3,]    3    9
>
> Ryszard Czerminski   phone: (781)994-0479
> ArQule, Inc.         email:ryszard at arqule.com
> 19 Presidential Way  http://www.arqule.com
> Woburn, MA 01801     fax: (781)994-0679
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
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