I have a file like this: id n1 n2 n3 n4 n5 n6 1 3 4 7 8 10 2 2 4 1 2 4 3 10 3 7 0 0 0 0 8 4 10 1 0 0 2 3 5 11 1 0 0 0 5 what I want to do is: only if n2=0 and n3=0 and n4=0 and n5=0 then delete the row. how can I do that? thank you, karena -- View this message in context: http://n4.nabble.com/a-question-about-deleting-rows-tp1013403p1013403.html Sent from the R help mailing list archive at Nabble.com.
yourdataframe = subset(yourdataframe, !(n2==0 & n3==0 & n4==0 & n5==0))>>>From: karena <dr.jzhou@gmail.com> To:<r-help@r-project.org> Date: 14/Jan/2010 12:24 p.m. Subject: [R] a question about deleting rows I have a file like this: id n1 n2 n3 n4 n5 n6 1 3 4 7 8 10 2 2 4 1 2 4 3 10 3 7 0 0 0 0 8 4 10 1 0 0 2 3 5 11 1 0 0 0 5 what I want to do is: only if n2=0 and n3=0 and n4=0 and n5=0 then delete the row. how can I do that? thank you, karena -- View this message in context: http://n4.nabble.com/a-question-about-deleting-rows-tp1013403p1013403.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R ( http://www.r/ )-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]
Try this:> xid n1 n2 n3 n4 n5 n6 1 1 3 4 7 8 10 2 2 2 4 1 2 4 3 10 3 3 7 0 0 0 0 8 4 4 10 1 0 0 2 3 5 5 11 1 0 0 0 5> delete <- with(x, n2 == 0 & n3 == 0 & n4 == 0 & n5 == 0) > delete[1] FALSE FALSE TRUE FALSE FALSE> x[!delete,]id n1 n2 n3 n4 n5 n6 1 1 3 4 7 8 10 2 2 2 4 1 2 4 3 10 4 4 10 1 0 0 2 3 5 5 11 1 0 0 0 5>On Wed, Jan 13, 2010 at 5:15 PM, karena <dr.jzhou@gmail.com> wrote:> > I have a file like this: > id n1 n2 n3 n4 n5 n6 > 1 3 4 7 8 10 2 > 2 4 1 2 4 3 10 > 3 7 0 0 0 0 8 > 4 10 1 0 0 2 3 > 5 11 1 0 0 0 5 > > what I want to do is: only if n2=0 and n3=0 and n4=0 and n5=0 then delete > the row. how can I do that? > > thank you, > > karena > -- > View this message in context: > http://n4.nabble.com/a-question-about-deleting-rows-tp1013403p1013403.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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<http://www.r-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? [[alternative HTML version deleted]]
Hi r-help-bounces at r-project.org napsal dne 13.01.2010 23:15:05:> > I have a file like this: > id n1 n2 n3 n4 n5 n6 > 1 3 4 7 8 10 2 > 2 4 1 2 4 3 10 > 3 7 0 0 0 0 8 > 4 10 1 0 0 2 3 > 5 11 1 0 0 0 5 > > what I want to do is: only if n2=0 and n3=0 and n4=0 and n5=0 thendelete> the row. how can I do that?Why do you complicate things for yourself. Few days ago you wanted put zeroes instead of NA values. R has quite extensive capabilities how to handle NA values so your.na.data <- your.data[your.data==0]<-NA # returns NA instead zero values. chosen.one <- complete.cases(your.na.data[,2:5]) # makes a logical vector that is TRUE only if your.na.data does not have NA value in it. your.data[chosen.one,] or your.na.data[chosen.one,] # selects rows without NA values. Or you can use na.omit, na.rm or other NA handling facility provided with many functions. What about to read few pages from R intro manual where you can find how to start with data manipulation. Regards Petr> > thank you, > > karena > -- > View this message in context:http://n4.nabble.com/a-question-about-deleting-> rows-tp1013403p1013403.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
thank all of you for the help. to Petr: thanks for the suggestion, I will read the R intro manual. :-) -- View this message in context: http://n4.nabble.com/a-question-about-deleting-rows-tp1013403p1014267.html Sent from the R help mailing list archive at Nabble.com.