Skip to content

ordering a matrix by multiple columns

3 messages · Michael Dondrup, Peter Dalgaard, Uwe Ligges

#
Dear all,

is there an easy way to sort or get the order of a matrix or data.frame
by multiple rows 
(like ODER BY in SQL), that means if some values in the first column are
equal, then sort them (or 'order') by the second etc.? 

Thanks in advance!
Michael
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Michael Dondrup <Michael.Dondrup at Genetik.Uni-Bielefeld.DE> writes:
Oder was? (Sorry)

Try 

d[order(d$var1,d$var2,d$var3),] 

or 

d[with(d,order(var1,var2,var3)),]

The latter form is useful if d is called something longer than "d"...

The only mildly annoying thing is that you cannot (easily) sort
descending on character variables -- for numeric ones, just switch the
sign.
#
Michael Dondrup wrote:
What about looking in the help files? My first idea would be to take a
look at ?sort and ?order, two words you already mentioned in your
querstion. And I'd bet you'll get the answer.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._