Re: [R] Need advise in grab the line number of sorted list in R

From: Jason Lee <huajie.lee_at_gmail.com>
Date: Tue, 03 Jun 2008 10:37:33 +1000

Hi,

If there are more than one item having the same value, how can i use R to take the average of the position. E.g:-

       X Y Z

131 22.2 3.4   4.4
132 20.0 3.4   4.4==>Position 2 if queried
150  20.0 12.2 4.5 =>Position 3 if queried
134 20.0 3.4   4.4 ==> Position4 if queried
159  20.0 12.2 4.5==>Position 5 if queried
160  22.0 12.2 4.5

When I query 150 using which, it gives "3" but since in other position the value are same which is 20.0, how can i get the average of the position which is (2+3+4+5)/3

Is there any R package which I can use for the above calculation?

On Tue, Jun 3, 2008 at 12:02 AM, Marc Schwartz <marc_schwartz_at_comcast.net> wrote:

> on 06/02/2008 08:45 AM Jason Lee wrote:
>
>> Hi,
>>
>> I have a data frame which format is like below:-
>>
>> X Y Z
>> 131 22.2 3.4 4.4
>> 150 20.0 12.2 4.5
>>
>> etc...
>>
>> And I have sorted the data frame. However, I would like to grab one of
>> these
>> elements in the data frame. Also, i would like to number these rows so
>> that
>> when i grab a particular line let say 150, it is able to return me the
>> position of the row rather than the "150". Let say in this example, when i
>> grab a value of 150, it gives me 2 rather than 150.
>>
>>
>> I tried to add anohter extra column of number which is data$NUMBER =
>> row.names(data) on the sorted data but it seems not giving what I wanted
>> which is the "position" of the row 150.
>>
>> Please advise. Thanks.
>>
>
> Is this what you want?
>
> > DF
> X Y Z
> 131 22.2 3.4 4.4
> 150 20.0 12.2 4.5
>
>
> > which(row.names(DF) == 150)
> [1] 2
>
>
> See ?which
>
> HTH,
>
> Marc Schwartz
>
>

        [[alternative HTML version deleted]]



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. Received on Tue 03 Jun 2008 - 00:50:05 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 03 Jun 2008 - 08:30:37 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive