[R] remove missing values from matrix or data frame

Berton Gunter gunter.berton at gene.com
Tue Nov 9 23:31:33 CET 2004



?na.omit  

as in A<-na.omit(A)

-- Bert Gunter
Genentech Non-Clinical Statistics
South San Francisco, CA
 
"The business of the statistician is to catalyze the scientific learning
process."  - George E. P. Box
 
 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Spencer Graves
> Sent: Tuesday, November 09, 2004 2:19 PM
> To: William Briggs
> Cc: r-help at stat.math.ethz.ch
> Subject: Re: [R] remove missing values from matrix or data frame
> 
>       How about the following: 
> 
>  > (A <- array(c(1, NA, 3, NA, 4, 5), dim=c(3,2)))
>      [,1] [,2]
> [1,]    1   NA
> [2,]   NA    4
> [3,]    3    5
>  > A[apply(A, 1, function(x)!any(is.na(x))), , drop=F]
>      [,1] [,2]
> [1,]    3    5
> 
>       hope this helps.  spencer graves
> 
> William Briggs wrote:
> 
> >
> > Is there any way besides looping to remove complete rows 
> from a matrix 
> > or data frame where there is at least one NA in any of the columns?
> >
> > For example
> > > a
> >        [,1]     [,2]
> >   [1,] 0        2.6875
> >   [2,] 8.366667 6.625
> >   [3,] 15.6     4.375
> >   [4,] 23.4     6.25
> >   [5,] 29       5.09375
> >   [6,] 18       NA
> >   [7,] 0        4.15625
> >   [8,] 9.366667 6.25
> >   [9,] 14.73333 5.875
> >  [10,] 31.26667 6.15625
> >  [11,] NA       2.357
> >  [12,] NA       5.4234
> >  [13,] 0        3.34375
> >  [14,] 7.666667 2.78125
> >  [15,] NA       NA
> >
> > In a, rows 6, 11, 12, and 15 should be removed.
> >
> > na.omit(a) does nothing, nor does na.omit(as.data.frame(a)).  I can 
> > get a matrix of which are NA and not by "i<-!is.na(a)", but this 
> > doesn't seem to help ("a[i]" isn't the thing I'm after).
> >
> > I know I am missing something simple and standard, but I 
> haven't been 
> > able to see it yet (nor on Google).
> >
> > Thanks.
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide! 
> > http://www.R-project.org/posting-guide.html
> 
> 
> -- 
> Spencer Graves, PhD, Senior Development Engineer
> O:  (408)938-4420;  mobile:  (408)655-4567
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! 
> http://www.R-project.org/posting-guide.html
> 


More information about the R-help mailing list