Hello all, I'm trying to grasp a way to order a matrix by giving order() only a vector of the columns it can look to for sorting. The approach has to be generic such that I can feed order() a vector of variable length (ie varying # of columns to sort by). x<-rep(1,9) y<-c(1, 1, 2, 2, 2, 1, 2, 2, 2) z<-c(10, 11, 10, 13, 15,1000,1000, 1001,1002) results<-cbind(x,y,z) #pseudocode: # results[order(vector of column numbers),] # order desired: results[order(results[,1],results[,2],results[,3]),] __________________________________________________________________ thanks! Michael _______________________________________________________ Michael Folkes Salmon Stock Assessment Canadian Dept. of Fisheries & Oceans Pacific Biological Station [[alternative HTML version deleted]]
On Thu, Dec 9, 2010 at 5:24 PM, Folkes, Michael <Michael.Folkes at dfo-mpo.gc.ca> wrote:> Hello all, > I'm trying to grasp a way to order a matrix by giving order() only a vector of the columns it can look to for sorting. > The approach has to be generic such that I can feed order() a vector of variable length (ie varying # of columns to sort by). > > > x<-rep(1,9) > y<-c(1, 1, 2, 2, 2, 1, 2, 2, 2) > z<-c(10, 11, 10, 13, ?15,1000,1000, 1001,1002) > results<-cbind(x,y,z) > > #pseudocode: > # results[order(vector of column numbers),] > > # order desired: > results[order(results[,1],results[,2],results[,3]),]Try this: ix <- 1:3 results[do.call(order, as.data.frame(results)[ix]), ] as.data.frame can optionally be omitted if the results object is already a data frame (in your example code its a matrix). -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com
Possibly Parallel Threads
- lattice: passing multiple lty values to the key/legend
- Hmisc binconf function value interpretation during narrow confidence intervals
- Floating points and floor() ?
- RODBC excel - need to preserve (or extract) numeric column names
- Convert week value to date