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 1 2 1 1 2 2 2 3 3 2 3 5 5 4 2 then the result should be 1 1 1 2 2 2 3 3 2 2 3 4 5 2 5 Let ONE be a vector of 1's of length ncol(X). The result in this example can be accomplished by X=X[,order(ONE,X[1,],X[,2],X[,3])] 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. Thanks, JP [[alternative HTML version deleted]]
"?order"? J. P. Morgan 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 > > > > 1 2 1 1 2 > > 2 2 3 3 2 > > 3 5 5 4 2 > > > > then the result should be > > > > 1 1 1 2 2 > > 2 3 3 2 2 > > 3 4 5 2 5 > > > > Let ONE be a vector of 1's of length ncol(X). The result in this example can > be accomplished by > > > > X=X[,order(ONE,X[1,],X[,2],X[,3])] > > > > 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. > > > > Thanks, JP > > > > > > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
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: 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
"J. P. Morgan" <jpmorgan at vt.edu> writes:> 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 > > > > 1 2 1 1 2 > > 2 2 3 3 2 > > 3 5 5 4 2 > > > > then the result should be > > > > 1 1 1 2 2 > > 2 3 3 2 2 > > 3 4 5 2 5 > > > > Let ONE be a vector of 1's of length ncol(X). The result in this example can > be accomplished by > > > > X=X[,order(ONE,X[1,],X[,2],X[,3])]What's the ONE supposed to be good for??? Works just as well without (after fixing the obvious typo):> X[,order(X[1,],X[2,],X[3,])]V1 V4 V3 V5 V2 1 1 1 1 2 2 2 2 3 3 2 2 3 3 4 5 2 5> 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.This should do it:> X[,do.call("order",split(X,row(X)))]V1 V4 V3 V5 V2 1 1 1 1 2 2 2 2 3 3 2 2 3 3 4 5 2 5 (is there a better way of obtaining a list containing the rows of a matrix?) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907