[BioC] How to sort a matrix based on its column names and preserving the identical column names

Joern Toedling toedling at ebi.ac.uk
Fri Aug 3 15:04:23 CEST 2007


Hi Carol,

despite the fact that it is certainly preferable to have unique column
names and this can prevent all sorts of hard-to-debug confusion later on,
I think there is something missing from your example code.

If you really only have a matrix and only do some resorting of the
colums you will definitely keep the original colum names,
check this example:

> A = matrix(rnorm(6),nrow=2)
> colnames(A)<-c("bla","bal","bla")
> A
            bla        bal       bla
[1,] -1.1283559 -0.5672175 -1.135924
[2,] -0.8160838 -0.7441979  1.395936
> (A[,sort(colnames(A))])
            bal        bla        bla
[1,] -0.5672175 -1.1283559 -1.1283559
[2,] -0.7441979 -0.8160838 -0.8160838

Actually, you will notice that the second and third column are
identical, since after the sorting the first occurrence of the column
name is taken from A.
To prevent that you should not index the matrix using a character vector
but the numeric indices (or use unique column names):

> (A[,order(colnames(A))])
          bal        bla        bla
[1,] -0.5672175 -1.1283559  -1.135924
[2,] -0.7441979 -0.8160838  1.395936

Renaming of column names, however, only happens when you convert the
matrix into a data.frame:
> data.frame(A)
         bla        bal     bla.1
1 -1.1283559 -0.5672175 -1.135924
2 -0.8160838 -0.7441979  1.395936

and this renaming can be prevented by setting the argument check.names=FALSE
> data.frame(A, check.names=FALSE)
         bla        bal       bla
1 -1.1283559 -0.5672175 -1.135924
2 -0.8160838 -0.7441979  1.395936

Best regards,
Joern


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
>



More information about the Bioconductor mailing list