[R] lexicographic sort of ordered lists
Prof Brian Ripley
ripley at stats.ox.ac.uk
Fri Jul 18 22:52:56 CEST 2003
On Fri, 18 Jul 2003, Duncan Murdoch wrote:
> On Fri, 18 Jul 2003 15:56:31 -0400, "J. P. Morgan" <jpmorgan at vt.edu>
> wrote :
>
> >Does anyone know how to execute the following sort problem in R? Matrix X
> >has positive integer entries, and each column has been sorted in ascending
> >order. The problem is now to order the columns lexicographically. For
> >instance if the matrix X is
>
> ...
>
> >but I need a method that will work regardless of k=number of rows of X. That
> >is, the program must be able to accept any integer-valued matrix X for which
> >each column is sorted, then permute columns accordingly.
>
> You could do it in a loop, if you had a "stable" order() function. I
> don't know if the standard order() is stable; here's one that is
> stable:
It is: why is that not on the help page, I wonder (nor for sort)?
>
> stableorder <- function(x, ...) order(x, ...,1:length(x))
>
> Then the idea is to loop from the last row to the first, sorting
> columns in a stable way:
>
> lexico <- function(X) {
> for (i in nrow(X):1) {
> X <- X[, stableorder(X[i,])]
> }
> X
> }
>
> (I just tried this with the regular order(), and got the same result,
> so order() might be stable, but I wouldn't trust it to be...)
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>
--
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