[R] about the function order()

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Nov 27 15:41:27 CET 2001


On Tue, 27 Nov 2001, Emmanuel Charpentier wrote:

> According to Thomas Lumley :
>
>  >In a sense it's doing the opposite of what you thought.
>
>  >The definition of order() is basically that
>  >   a[order(a)]
>  >is in increasing order. This works with your example, where the correct
>  >order is the fourth, second, first, then third element.
>
>  >You may have been looking for rank(), which returns the rank of the
>  >elements
> />R> a <- c(4.1, 3.2, 6.1, 3.1) /
> />R> order(a) /
>  >[1] 4 2 1 3
> />R> rank(a) /
>  >[1] 3 2 4 1
>  >so rank() tells you what order the numbers are in, order() tells you how
>  >to get them in ascending order.
>
> Hmmm ... meaning that order behaves like the (gradeup) APL function,
> right ? (Yes, I'm *that* old ...).
>
> That should imply that, barring possible ties, rank(x) ==
> order(order(x)). Right ?
>
> So : why distinc implementations ? Are there efficiency considerations
> I'm missing ?

Well, ties, as you mentioned.  rank averages ties.

Also, porting S code needs both, as S has both.  We've even added
sort.list to facilitate that.  (sort.list in S is a lighter-weight version
of order for just one vector as used here.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list