# [R] Programming puzzle...

Rashid Nassar rnassar at duke.edu
Thu Jul 13 16:57:50 CEST 2000

```Here is a small function that was posted here some time ago by Renaud
Lancelot in response to a similar question and which I find very handy:

Sortmat.R <-
function (Mat, Sort)
#  by   Renaud Lancelot <lancelot at telecomplus.sn>
#  Sort matrix or dataframe 'Mat', by column(s) 'Sort'.
#  e.g. sortmat.R(datafr, c(2,4,1))
{
m <- do.call("order", as.data.frame(Mat[, Sort]))
Mat[m, ]
}

On Thu, 13 Jul 2000, Ron Wehrens wrote:

> Date: Thu, 13 Jul 2000 15:37:24 +0200 (MET DST)
> From: Ron Wehrens <rwehrens at sci.kun.nl>
> To: Rhelp <r-help at hypatia.math.ethz.ch>
> Subject: [R] Programming puzzle...
>
> Dear R guru's,
>
> I'd like to sort the columns of a matrix according to the elements in the
> first row, and in case of a tie according to the second row, etc.
> In case of a 3xn matrix woppa, something like
>
> colorder _ order(woppa[1,], woppa[2,], woppa[3,])
>
> I just cannot seem to find a way to do this for a variable number of rows,
> e.g. if woppa has 10 or 15 rows I now manually cut and paste to have 10 - 15
> arguments in the order function. There has to be a clever way to do this...
>
>
> Ron
>
> -----------------------+--------------------------------------------------
>  Ron Wehrens            | " In a purely metaphorical sense, I am a turnip "
>   Dept. of Chemometrics  |                         (W.A. Quayle)
>    University of Nijmegen |   Email: rwehrens at sci.kun.nl
>     Toernooiveld 1         |   http://www-cac.sci.kun.nl/cac/rwehrens
>      6525 ED Nijmegen       |   Tel: +31 24 365 2053
>       The Netherlands        |   Fax: +31 24 365 2653
>        -----------------------+------------------------------------------------
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> 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
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>
>
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```