[R] Compute rank within factor groups

Greg Snow Greg.Snow at intermountainmail.org
Mon Jul 16 18:35:14 CEST 2007

The order and rank functions do something of an inverse of each other.
The rank function tells you the rank of each element of the vector (if
the first element is the 2nd smallest, then the first element of the
return in 2).  The order function tells you what order to put the vector
in to sort it (if the smallest element of the vector is in position 3,
then the first element of the returned vector will be 3).  Doing
something like:

> order(order(x))

Is similar to rank, except in how it deals with ties.

When the data is presorted, then rank and order both give you the same
as seq(along=x) which is just the set of integers from 1 to the length
of the vector.  

Most computations in R will switch between integer and double
automatically, but if you really need a vector to be integer, then use
the as.integer function.

Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at intermountainmail.org
(801) 408-8111

> -----Original Message-----
> From: Ken Williams [mailto:ken.williams at thomson.com] 
> Sent: Thursday, July 12, 2007 3:50 PM
> To: Greg Snow; R-help at stat.math.ethz.ch
> Subject: Re: [R] Compute rank within factor groups
> On 7/12/07 4:28 PM, "Greg Snow" 
> <Greg.Snow at intermountainmail.org> wrote:
> > Why are you using order instead of rank?
> > 
> > If the data is pre sorted then they tend to give the same result 
> > (unless there are ties), but if your data is not presorted, 
> then the 
> > results will be different.
> Indeed, thanks for the catch.  I switched to order because 
> rank was giving me floats instead of integers, which I now 
> see was probably because it defaults to ties.method=average 
> and I wanted ties.method=first.
> My data was indeed pre-sorted so I didn't notice the 
> difference (are they giving inverse permutations or 
> something? Can't quite follow...), but perhaps it won't always be.
> --
> Ken Williams
> Research Scientist
> The Thomson Corporation
> Eagan, MN

More information about the R-help mailing list