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.
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
Prof Brian Ripley
2004-Nov-09 22:29 UTC
[R] remove missing values from matrix or data frame
Something is not as it seems:> a <- matrix(scan(),,2,byrow=T)1: 0 2.6875 3: 8.366667 6.625 5: 15.6 4.375 7: 23.4 6.25 9: 29 5.09375 11: 18 NA 13: 0 4.15625 15: 9.366667 6.25 17: 14.73333 5.875 19: 31.26667 6.15625 21: NA 2.357 23: NA 5.4234 25: 0 3.34375 27: 7.666667 2.78125 29: NA NA 31: Read 30 items and a looks like yours and> na.omit(a)[,1] [,2] [1,] 0.000000 2.68750 [2,] 8.366667 6.62500 [3,] 15.600000 4.37500 [4,] 23.400000 6.25000 [5,] 29.000000 5.09375 [6,] 0.000000 4.15625 [7,] 9.366667 6.25000 [8,] 14.733330 5.87500 [9,] 31.266670 6.15625 [10,] 0.000000 3.34375 [11,] 7.666667 2.78125 attr(,"na.action") [1] 11 12 15 6 attr(,"class") [1] "omit" does something, in fact what you asked for. So what is a? What does str(a) say about it? On Tue, 9 Nov 2004, 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 > >-- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
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/