[R] order()

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Mar 29 12:23:40 CET 2002

On Fri, 29 Mar 2002, Ko-Kang Kevin Wang wrote:

> Hi,
> In the order() help file, there is an example like:
>      a <- c(4, 3, 2, NA, 1)
>      b <- c(4, NA, 2, 7, 1)
>      z <- cbind(a, b)
>      (o <- order(a, b)); z[o, ]
> How can I do something like "order a in ascending order, b in descending
> order"?  And say I have a third vector c, and I'd like to add this to the

What order does is to use lexicopgraphic order, that is order by a, break
ties by b.  As from 1.5.0 it will allow decreasing ordering, but for all

> previous condition "a ascending, b descending, c descending".  How can I
> do this?

Only (at all easily) by arranging b and c to be in reverse order, for
examply by using -b if b is numeric.  And you can easily do this, since

order(a, b) = order(rank(a), rank(b))

so in your example use

order(a, -rank(b), -rank(c))

I'd be intrigued to know why you want this.

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