[BioC] How to sort a matrix based on its column names and preserving the identical column names
Oleg Sklyar
osklyar at ebi.ac.uk
Fri Aug 3 15:18:52 CEST 2007
mat[, order(colnames(mat))]
order provides indexes of sorted elements without affecting their names.
Sort works in your case half ways only because you are sorting
characters, and columns can be identified by characters, otherwise it is
wrong.
Here is the working example:
> x<-c("col","abc","def","abc","col")
> a<-matrix(runif(25),ncol=5,nrow=5)
> colnames(a) <- x
> a
col abc def abc col
[1,] 0.9815985 0.65855865 0.9982046 0.07781167 0.3228944
[2,] 0.5970836 0.19195563 0.9082061 0.80489513 0.9190933
[3,] 0.8147790 0.13499074 0.9431437 0.41154237 0.6487952
[4,] 0.7661668 0.26216671 0.6694043 0.66462428 0.2177653
[5,] 0.5604505 0.04371932 0.7873665 0.44849293 0.1700327
> a[,order(x)]
abc abc col col def
[1,] 0.65855865 0.07781167 0.9815985 0.3228944 0.9982046
[2,] 0.19195563 0.80489513 0.5970836 0.9190933 0.9082061
[3,] 0.13499074 0.41154237 0.8147790 0.6487952 0.9431437
[4,] 0.26216671 0.66462428 0.7661668 0.2177653 0.6694043
[5,] 0.04371932 0.44849293 0.5604505 0.1700327 0.7873665
> order(x)
[1] 2 4 1 5 3
On Fri, 2007-08-03 at 05:36 -0700, carol white wrote:
> Hello,
> How to sort a matrix based on its column names and preserving the identical column names.
>
> when I use mat [, sort(colnames(mat))], sort changes all column names to unique ones. for ex, if the name of 2 columns is col, the 2nd will be changed to col.1 whereas I want to keep the col name for the two columns
>
> col col -> col col.1
>
> thanks
>
>
> ---------------------------------
> Park yourself in front of a world of choices in alternative vehicles.
>
> [[alternative HTML version deleted]]
>
> _______________________________________________
> Bioconductor mailing list
> Bioconductor at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/bioconductor
> Search the archives: http://news.gmane.org/gmane.science.biology.informatics.conductor
--
Dr. Oleg Sklyar * EBI-EMBL, Cambridge CB10 1SD, England * +44-1223-494466
More information about the Bioconductor
mailing list