Thanks Gabor,
I should have paid closer to the examples within the function help.  I discovered the same approach minutes after submission.  :\

problem solved!

_______________________________________________________
Michael Folkes
Salmon Stock Assessment
Canadian Dept. of Fisheries & Oceans     
Pacific Biological Station
3190 Hammond Bay Rd.
Nanaimo, B.C., Canada
V9T-6N7
Ph (250) 756-7264 Fax (250) 756-7053  Michael.Folkes@dfo-mpo.gc.ca



-----Original Message-----
From: Gabor Grothendieck [mailto:ggrothendieck@gmail.com]
Sent: Thu 09/12/2010 2:34 PM
To: Folkes, Michael
Cc: r-help@r-project.org
Subject: Re: [R] order matrix by column position

On Thu, Dec 9, 2010 at 5:24 PM, Folkes, Michael
<Michael.Folkes@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


	[[alternative HTML version deleted]]

