[R] sort matrix by sum of columns

john seers (IFR) john.seers at bbsrc.ac.uk
Thu Jun 22 11:48:46 CEST 2006


Albert

Is this what you want?:

 a[,order(colSums(a))]


John S

 
---


-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Albert Vilella
Sent: 21 June 2006 11:38
To: r help
Subject: [R] sort matrix by sum of columns


Hi all,

I would like to know how can I sort the cols of a matrix by the sum of
their elements.


a <- matrix(as.integer(rnorm(25,4,2)),10,5)
colnames(a) = c("alfa","bravo","charlie","delta","echo")

I guess I should use colSums, and then rearrange the matrix somehow
according to the result.

My idea is to display a "sorted" barplot:

barplot(a, horiz=TRUE, legend.text=T)

Thanks in advance,

    Albert.

______________________________________________
R-help at stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html



More information about the R-help mailing list