# [R] sort (all columns of) a matrix

Bert Gunter gunter.berton at gene.com
Thu Oct 8 20:41:07 CEST 2009

```Right. My guess is that  Kajan wants:

a[do.call(order,data.frame(a)),]
## this generalizes to an arbitrary number of columns
## do.call() is a very powerful and useful R feature worth learning about

Yet another reason why the posting guide asks for a simple, proper,
reproducible example.

-- Bert

Bert Gunter
Genentech Nonclinical Biostatistics

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Erik Iverson
Sent: Thursday, October 08, 2009 11:28 AM
To: Kajan Saied; r-help at r-project.org
Subject: Re: [R] sort (all columns of) a matrix

You need to define "sorted" in the context of a matrix.  By each row
individually, by each column individually, or by column using the whole
matrix, or by row using the whole matrix?

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Kajan Saied
> Sent: Thursday, October 08, 2009 11:51 AM
> To: r-help at r-project.org
> Subject: [R] sort (all columns of) a matrix
>
> Dear R-Help Team,
>
> I have been trying to sort (all columns of) a matrix:
>
> > a<-matrix(a<-c(1,3,4,6,6,4,6,56,4,64,86,39,4,2),length(a),2)
> > a
>       [,1] [,2]
>  [1,]    1    1
>  [2,]    3    3
>  [3,]    4    4
>  [4,]    6    6
>  [5,]    6    6
>  [6,]    4    4
>  [7,]    6    6
>  [8,]   56   56
>  [9,]    4    4
> [10,]   64   64
> [11,]   86   86
> [12,]   39   39
> [13,]    4    4
> [14,]    2    2
> >
> I would like to have the matrix sorted and the output should again be *a
> matrix* with nrow=length(a) and ncol=2 just like (a) but only
> sorted. Whatever I try (be it sort(), order()...) I receive a vector of
> length(a)*2...
>
>
> Best regards,
>
> kajan
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help