[R] remove missing values from matrix or data frame

Roger D. Peng rpeng at jhsph.edu
Tue Nov 9 23:32:13 CET 2004


You might be interested in complete.cases(), as in:

use <- complete.cases(a)
a[use, ]

-roger

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
> 

-- 
Roger D. Peng
http://www.biostat.jhsph.edu/~rpeng/




More information about the R-help mailing list