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 [[alternative HTML version deleted]]
See ?complete.cases and perhaps ?na.omit. You can index the matrix by the output of complete.cases() for the first case, and feed the matrix without the omitted column to complete.cases() for the others. Andy From: mark salsburg> > 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 > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 > >
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}}
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 >
Dear Colleagues: I have a question that is probably too simple but I could not get it. How to remove all the rows of a data table with missing values? Let us say my data table is 1 2 4 3 . 5 2 7 . 6 7 8 The result should be, 1 2 4 6 7 8 Thank you, Ashraf ---------------- _________________________________ M. Ashraf Chaudhary, Ph.D. Department of International Health Johns Hopkins Bloomberg School of Public Health 615 N. Wolfe St. Room W5506 Baltimore MD 21205-2179 (410) 502-0741/fax: (410) 502-6733 <mailto:mchaudha@jhsph.edu> mchaudha@jhsph.edu [[alternative HTML version deleted]]
Check out ?na.omit ?complete.cases On 3/21/06, Ashraf Chaudhary <mchaudha at jhsph.edu> wrote:> Dear Colleagues: > > I have a question that is probably too simple but I could not get it. > > How to remove all the rows of a data table with missing values? > > Let us say my data table is > > 1 2 4 > 3 . 5 > 2 7 . > 6 7 8 > > The result should be, > > 1 2 4 > 6 7 8 > > Thank you, > > Ashraf > > > ---------------- > > _________________________________ > M. Ashraf Chaudhary, Ph.D. > Department of International Health > Johns Hopkins Bloomberg School of Public Health > 615 N. Wolfe St. Room W5506 > Baltimore MD 21205-2179 > > (410) 502-0741/fax: (410) 502-6733 > <mailto:mchaudha at jhsph.edu> mchaudha at jhsph.edu > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > 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 >