[R] removing ROWS with missing values
Adaikalavan Ramasamy
ramasamy at cancer.org.uk
Fri Mar 17 02:10:54 CET 2006
My answers are going to be very similar but with minor cosmetic changes
that hopefully will make it bit more clearer.
1) How do you read in the data ? If you are using read.table (or
read.csv, read.delim, etc) you can set na.strings="-999" to take
advantage of the R's missing value features.
2) First count how many missing values. Then subset to the rows with at
least 6 numerical values:
number.present <- rowSums( myMatrix != -999 )
good.rows <- which( number.present >= 6 )
myMatrix.sub <- myMatrix[ good.rows, ]
Note : change the first line to rowSums( !is.na( myMatrix ) ) if you
have coded missing values properly as in comment 1).
Regards, Adai
On Thu, 2006-03-16 at 21:45 +0100, plummer at iarc.fr wrote:
> 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.)
>
> Martyn
> -----------------------------------------------------------------------
> This message and its attachments are strictly confidential. ...{{dropped}}
>
> ______________________________________________
> 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