> -----Original Message----- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Jason Rupert > Sent: Tuesday, May 26, 2009 1:55 PM > To: Mark Wardle > Cc: R-help@r-project.org > Subject: Re: [R] Problem accessing "row number" from subset > on a dataframe > > > Mark, > > I really apprecaite your response and continue to be amazed > by the responsiveness and support on the R forums. > > And, well actually, I would like to get the "row number"(s) > and then delete or not via the row number.
Deleting by row number can lead to incorrect answers if you are not careful to dispose of the no-rows case specially, since when length(rowNumbers)==0, x[rowNumbers,] and x[-rowNumbers,] will return the same thing, the entire matrix or data.frame. If you use logical subscripts this is not a special case, you just use x[!rowSatisfiesCondition,] whether or not any row satisfies the condition. (Sometimes using integer instead of logical subscripts can save memory, but you need a pretty big problem to notice that.) > d<-data.frame(x=11:14,y=letters[11:14]) > xIsFive <- d$x==5 > whichXIsFive <- which(xIsFive) > d[ !xIsFive, ] # correct x y 1 11 k 2 12 l 3 13 m 4 14 n > d[ -whichXIsFive, ] # not what is wanted [1] x y <0 rows> (or 0-length row.names) > Again, I really appreciate the response... > > --- On Tue, 5/26/09, Mark Wardle <m...@wardle.org> wrote: > > > From: Mark Wardle <m...@wardle.org> > > Subject: Re: [R] Problem accessing "row number" from subset > on a dataframe > > To: "Jason Rupert" <jasonkrup...@yahoo.com> > > Cc: R-help@r-project.org > > Date: Tuesday, May 26, 2009, 3:18 PM > > Hi. I may be missing what you're > > trying to achieve, but... > > > > what about > > > > subset(airquality, airquality$Month!=6) > > > > instead? > > > > You can do arbitrarily complex queries if you wish, > > combining terms logically. > > > > You don't have to use the subset function. You may find it > > helpful to > > see what the following result in: > > > > airquality$Month==6 > > airquality[airquality$Month==6, ] > > airquality[airquality$Month==6, ] > > > > There are ways of getting the row numbers, but I suspect > > you don't > > actually need to do that, do you? > > > > Best wishes, > > > > Mark > > > > 2009/5/26 Jason Rupert <jasonkrup...@yahoo.com>: > > > > > > > > > I would like to use the "row number" information > > returned from performing a subset command on a dataframe. > > > > > > For example, I would like to automatically delete some > > rows from a dataframe if they match a criteria. Here is my > > example below. > > > > > > data(airquality) > > > names(airquality) > > > subset(airquality, airquality$Month == 6) > > > > > > Now how do I delete the row numbers returned > > automatically? > > > > > > I know I can type > > > airquality_mod<-airquality[-c(32:60)] > > > > > > However, I would like to check the row information and > > then use it to delete the stuff out of the dataframe. > > > > > > Thank again for any feedback and insights. > > > > > > ______________________________________________ > > > 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. > > > > > > > > > > > > > > -- > > Dr. Mark Wardle > > Specialist registrar, Neurology > > Cardiff, UK > > > > > > > ______________________________________________ > 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. > ______________________________________________ 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.