[R] Function rank() for data frames (or multiple vectors)?

Sebastian Bauer Sebastian.Bauer at charite.de
Mon Aug 29 15:39:07 CEST 2011


Hi!

>> On 08/24/2011 07:46 PM, David Winsemius wrote:
>>>> I was looking for an elegant solution ;) In the real case I have double
>>>> values and this would be quite inefficient then.
>>>
>>> Still no r-code:
>>>
>>> Then what about rank(order(...) , further-ties.method-argument) ?
>>
>> I think that, as order() always gives a different value for each
>> element, rank(order()) would return the same result as order() alone.
>
> Quite right. I didn't test it since there was no example provided. Do
> you not understand what is meant by a reproducible example.

Sorry, I thought I gave an example in my response to your response. 
Didn't know that you wanted a R example (which I didn't have at that time)

> Pretty much every solution I come up with leaves me (re-) asking the
> question: What's wrong with rank(paste(...))?

As said, this is rather inefficient and moreover doesn't work for 
floats, for which the lexical order of the string representation doesn't 
match the natural order (e.g., "3e-10" is lexical smaller than "1e-13", 
while 3e-10 is larger than 1e-13).

> Here's another possibility:
>
>  > rr <- data.frame(a = c(1,1,1,1,2), b=c(1,2,2,3,1))
>
>  > ave(order(rr$a, rr$b), rr$a, rr$b )
> [1] 1.0 2.5 2.5 4.0 5.0

Actually, this may be a solution I was looking for! Note that it assumes 
that rr to be sorted already (hence the first argument of ave could be 
simply 1:nrow(rr)). Also, by using FUN=min or FUN=max I can cover the 
other cases. Thanks for this!

Bye,
Sebastian



More information about the R-help mailing list