[R] sort matrix by sum of columns

Petr Pikal petr.pikal at precheza.cz
Wed Jun 21 13:04:48 CEST 2006


Hi

order is your friend


On 21 Jun 2006 at 11:38, Albert Vilella wrote:

From:           	Albert Vilella <avilella at gmail.com>
To:             	r help <r-help at stat.math.ethz.ch>
Date sent:      	Wed, 21 Jun 2006 11:38:17 +0100
Subject:        	[R] sort matrix by sum of columns
Send reply to:  	avilella at gmail.com
	<mailto:r-help-request at stat.math.ethz.ch?subject=unsubscribe>
	<mailto:r-help-request at stat.math.ethz.ch?subject=subscribe>

> 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")

 ord<-order(colSums(a))
 barplot(a[,ord], horiz=TRUE, legend.text=T)

HTH
Petr


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

Petr Pikal
petr.pikal at precheza.cz



More information about the R-help mailing list