[R] order a matrix

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Nov 5 18:32:42 CET 2007


On Mon, 5 Nov 2007, Christian Hennig wrote:

> Dear list,
>
> order(x,y,z) returns a permutation to order x, ties broken by y, remaining
> ties broken by z. (And so on.)
>
> What I'd like to do is
> order(X), where X is a matrix (or a list or data frame if necessary) of
> unspecified size, which orders X[,1], ties broken by X[,2], remaining ties
> broken by X[,3] and so on - without having to know and to write down how
> many columns  X has.
>
> Any ideas how to achieve that?

For a list or data frame, do.call("order", X).
For a matrix, do.call("order", split(A, col(A))).

-- 
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 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



More information about the R-help mailing list