[R] removing ROWS with missing values

plummer at iarc.fr plummer at iarc.fr
Thu Mar 16 21:45:25 CET 2006

Quoting mark salsburg <mark.salsburg at gmail.com>:

> I am trying to find out if R can recognize specific criteria for removing
> rows (i.e. a prexisting function)
> I have a matrix myMatrix that is 12000 by 20
> I would like to remove rows from myMatrix that have:
> -999 across all columns
> -999 across all columns but one
> -999 across all columns but two
> -999 across all columns but three
> -999 across all columns but four
> -999 across all columns but five
> (-999 here is my missing value)
> Does R have a function for this, I've explored subset() so far

You can create a vector that records the number of missing values
in each row

n.notmissing <- apply(myMatrix != -999, 1, sum)

then use row subsetting to remove the ones you don't want

myMatrix[n.notmissing == n, ]

for n = 0, 1, ... 5, etc.

(As an aside, R functions will work better with your data if you use NA
instead of a numeric code to represent missing data.)

This message and its attachments are strictly confidential. ...{{dropped}}

More information about the R-help mailing list