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

Robert Gentleman rgentlem at fhcrc.org
Fri Aug 3 18:05:24 CEST 2007


Hi,
   I think that there is some confusion here. First sort does not change 
any values, it sorts. The changing of column and/or row names is "a 
feature", that comes with the [ operator.  Details are given on the man 
page for [.data.frame.

   I do not believe that there is any such behavior (currently) for 
matrices. And I could not replicate the behavior described by Carol 
except with data.frames, which is the documented (if peculiar) behavior.


  best wishes
    Robert

m=matrix(rnorm(25), nc=5)
colnames(m) = rep("A", 5)
m[,2]
m[1,]
m[,sort(colnames(m))]

but,
  y = data.frame(m)
  y
#changes the colnames
  y = data.frame(m, check.names=FALSE)

# but then do change the names
  y[1,]
  y[,sort(colnames(y))]



Oleg Sklyar wrote:
> 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

-- 
Robert Gentleman, PhD
Program in Computational Biology
Division of Public Health Sciences
Fred Hutchinson Cancer Research Center
1100 Fairview Ave. N, M2-B876
PO Box 19024
Seattle, Washington 98109-1024
206-667-7700
rgentlem at fhcrc.org



More information about the Bioconductor mailing list