Dear R-help team, I am getting addicted to using R but keep on getting many challenges on the way especially on data management (data cleaning). I have been wanting to drop all the rows if there values are `NA' or have specific values like 1 or 2 or 3. mdat <- matrix(1:21, nrow = 7, ncol=3, byrow=TRUE, dimnames = list(c("row1", "row2","row3","row4","row5","row6","row7"), c("C.1", "C.2", "C.3"))) mdat<-data.frame(mdat) mdat C.1 C.2 C.3 row1 1 2 3 row2 4 5 6 row3 7 8 9 row4 10 11 12 row5 13 14 15 row6 16 17 18 row7 19 20 21 I want to say drop row if value=1 or value =11 or value =20 How do I do that? Kind regards, Lazarus Mramba Junior Statistician P.O Box 986, 80108, Kilifi, Kenya Mobile No. +254721292370 Tel: +254 41 522063 Tel: +254 41 522390 (office extension : 419) This e-mail (including any attachment to it) contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. We apologize for any inconvenience that may have been caused to you. KEMRI-Wellcome Trust Programmecannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. KEMRI-Wellcome Trust Programme reserves the right to monitor all incoming and outgoing email traffic. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI- Wellcome Trust Programme".
Hi Lazarus, It would be more simple with mdat as a matrix (before coercing to a data.frame). It might be a simpler way to compare a matrix with a vector but I don't find it for the moment; in any case, this works: mdatT <- matrix(mdat %in% c(1, 11, 20), ncol=3)> mdat[!apply(mdatT, 1, any), ]C.1 C.2 C.3 row2 4 5 6 row3 7 8 9 row5 13 14 15 row6 16 17 18 Or you can use apply directly on a data.frame, with the same result: mdat <- as.data.frame(mdat)> mdat[!apply(mdat, 1, function(x) any(x %in% c(1, 11, 20))), ]C.1 C.2 C.3 row2 4 5 6 row3 7 8 9 row5 13 14 15 row6 16 17 18 hth, Adrian On Thursday 05 March 2009, Lazarus Mramba wrote:> Dear R-help team, > > I am getting addicted to using R but keep on getting many challenges on the > way especially on data management (data cleaning). > > I have been wanting to drop all the rows if there values are `NA' or have > specific values like 1 or 2 or 3. > > > mdat <- matrix(1:21, nrow = 7, ncol=3, byrow=TRUE, > dimnames = list(c("row1", > "row2","row3","row4","row5","row6","row7"), c("C.1", "C.2", "C.3"))) > mdat<-data.frame(mdat) > mdat > > C.1 C.2 C.3 > row1 1 2 3 > row2 4 5 6 > row3 7 8 9 > row4 10 11 12 > row5 13 14 15 > row6 16 17 18 > row7 19 20 21 > > I want to say drop row if value=1 or value =11 or value =20 > > How do I do that? > > > Kind regards, > Lazarus Mramba-- Adrian Dusa Romanian Social Data Archive 1, Schitu Magureanu Bd. 050025 Bucharest sector 5 Romania Tel.:+40 21 3126618 \ +40 21 3120210 / int.101 Fax: +40 21 3158391 [[alternative HTML version deleted]]
Have you looked at subset? For example, subset(mdat, !(mdat$C.1== 11)) You will have to add your other cases and there may be a better way to programmatically get it done, but maybe this will help you get started. --- On Thu, 3/5/09, Lazarus Mramba <LMramba@kilifi.kemri-wellcome.org> wrote: From: Lazarus Mramba <LMramba@kilifi.kemri-wellcome.org> Subject: [R] Dropping rows conditionally To: r-help@r-project.org Date: Thursday, March 5, 2009, 12:18 AM Dear R-help team, I am getting addicted to using R but keep on getting many challenges on the way especially on data management (data cleaning). I have been wanting to drop all the rows if there values are `NA' or have specific values like 1 or 2 or 3. mdat <- matrix(1:21, nrow = 7, ncol=3, byrow=TRUE, dimnames = list(c("row1", "row2","row3","row4","row5","row6","row7"), c("C.1", "C.2", "C.3"))) mdat<-data.frame(mdat) mdat C.1 C.2 C.3 row1 1 2 3 row2 4 5 6 row3 7 8 9 row4 10 11 12 row5 13 14 15 row6 16 17 18 row7 19 20 21 I want to say drop row if value=1 or value =11 or value =20 How do I do that? Kind regards, Lazarus Mramba Junior Statistician P.O Box 986, 80108, Kilifi, Kenya Mobile No. +254721292370 Tel: +254 41 522063 Tel: +254 41 522390 (office extension : 419) This e-mail (including any attachment to it) contains information which is confidential. It is intended only for the use of the named recipient. If you have received this e-mail in error, please let us know by replying to the sender, and immediately delete it from your system. Please note, that in these circumstances, the use, disclosure, distribution or copying of this information is strictly prohibited. We apologize for any inconvenience that may have been caused to you. KEMRI-Wellcome Trust Programmecannot accept any responsibility for the accuracy or completeness of this message as it has been transmitted over a public network. KEMRI-Wellcome Trust Programme reserves the right to monitor all incoming and outgoing email traffic. Although the Programme has taken reasonable precautions to ensure no viruses are present in emails, it cannot accept responsibility for any loss or damage arising from the use of the email or attachments. Any views expressed in this message are those of the individual sender, except where the sender specifically states them to be the views of KEMRI- Wellcome Trust Programme". ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
There are functions for this purpose: ?na.omit # dispays help page for na.fail, na.omit, na.exclude, na.pass -- David Winsemius On Mar 5, 2009, at 1:18 AM, Lazarus Mramba wrote:> > Dear R-help team, > > I am getting addicted to using R but keep on getting many challenges > on the way especially on data management (data cleaning). > > I have been wanting to drop all the rows if there values are `NA' > or have specific values like 1 or 2 or 3. > > > mdat <- matrix(1:21, nrow = 7, ncol=3, byrow=TRUE, > dimnames = list(c("row1", > "row2","row3","row4","row5","row6","row7"), > c("C.1", "C.2", "C.3"))) > mdat<-data.frame(mdat) > mdat > > C.1 C.2 C.3 > row1 1 2 3 > row2 4 5 6 > row3 7 8 9 > row4 10 11 12 > row5 13 14 15 > row6 16 17 18 > row7 19 20 21 > > I want to say drop row if value=1 or value =11 or value =20 > > How do I do that? > > > Kind regards, > Lazarus Mramba > Junior Statistician > P.O Box 986, 80108, > Kilifi, Kenya > Mobile No. +254721292370 > Tel: +254 41 522063 > Tel: +254 41 522390 > (office extension : 419) > > This e-mail (including any attachment to it) contains information > which is confidential. It is intended only for the use of the named > recipient. If you have received this e-mail in error, please let us > know > by replying to the sender, and immediately delete it from your system. > Please note, that in these circumstances, the use, disclosure, > distribution or copying of this information is strictly prohibited. We > apologize for any inconvenience that may have been caused to you. > KEMRI-Wellcome Trust Programmecannot accept any responsibility for > the accuracy > or completeness of this message as it has been transmitted over a > public > network. KEMRI-Wellcome Trust Programme reserves the right to > monitor all incoming and > outgoing email traffic. Although the Programme has taken reasonable > precautions to ensure no viruses are present in emails, it cannot > accept responsibility for any loss or damage arising from the use of > the > email or attachments. Any views expressed in this message are those of > the individual sender, except where the sender specifically states > them > to be the views of KEMRI- Wellcome Trust Programme". > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.