[R] Compute rank within factor groups

Greg Snow
Thu, 12 Jul 2007 21:28:25 -0000

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.

--
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 2:50 PM
> To: Greg Snow; R-help at stat.math.ethz.ch
> Subject: Re: [R] Compute rank within factor groups
>
>
>
>
> On 7/12/07 3:42 PM, "Ken Williams" <ken.williams at
thomson.com> wrote:
>
> > I ended up using:
> >
> >  wc$rank <- ave( wc$score, wc$report,
> >                  FUN=function(x) order(x, decreasing=TRUE) )
> >
> > Which gives me the 1-based rank integers I was looking for.
>
> Of course, immediately after sending I realized a simpler way:
>
>   wc$rank <- ave( -wc$score, wc$report, FUN=order )
>
> And as a newbie I think I get to be blissfully ignorant of
> which one is faster. =)
>
>
> --
> Ken Williams
> Research Scientist
> The Thomson Corporation
> Eagan, MN
>
>