[R] ordering a matrix by multiple columns

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Mon Mar 18 19:15:21 CET 2002


Michael Dondrup <Michael.Dondrup at Genetik.Uni-Bielefeld.DE> writes:

> 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.? 

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.

-- 
   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
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list