[R] Problem accessing "row number" from subset on a dataframe
Mark Wardle
mark at wardle.org
Tue May 26 23:16:47 CEST 2009
Ok, if you insist [although it's still unclear why you need this level
of indirection!]
Try ?which
e.g.
> which(airquality$Month==5)
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
23 24 25 26 27 28 29 30 31
> which(airquality$Month==6)
[1] 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
54 55 56 57 58 59 60 61
>
gives the vector indices. These can be used as row indices for the dataframe.
bw
Mark
2009/5/26 Jason Rupert <jasonkrupert at yahoo.com>:
>
> 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.
>
> Again, I really appreciate the response...
>
> --- On Tue, 5/26/09, Mark Wardle <mark at wardle.org> wrote:
>
>> From: Mark Wardle <mark at wardle.org>
>> Subject: Re: [R] Problem accessing "row number" from subset on a dataframe
>> To: "Jason Rupert" <jasonkrupert at yahoo.com>
>> Cc: R-help at 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 <jasonkrupert at 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 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.
>> >
>> >
>>
>>
>>
>> --
>> Dr. Mark Wardle
>> Specialist registrar, Neurology
>> Cardiff, UK
>>
>
>
>
>
>
--
Dr. Mark Wardle
Specialist registrar, Neurology
Cardiff, UK
More information about the R-help
mailing list