[R] getting column names of row-by-row sorted matrix

David Winsemius dwinsemius at comcast.net
Wed Sep 1 17:54:41 CEST 2010


On Sep 1, 2010, at 11:34 AM, David Winsemius wrote:

>
> snipped
> > t( apply(a, 1, function(x) colnames(a)[order(x)]) )
>     [,1] [,2] [,3] [,4]
> [1,] "A"  "C"  "B"  "D"
> [2,] "A"  "C"  "B"  "D"
> [3,] "D"  "B"  "C"  "A"
> [4,] "B"  "A"  "C"  "D"
> [5,] "C"  "D"  "B"  "A"
>
> (apply returns a transposed version.)

That is over-broad.  apply() only returns a "transposed" result of a  
matrix when the second argument is "1". It is (as I understand it  
anyway) supplying results for building another matrix in the default,  
for R,  column-major order. If you start with rows uou gt columns. If  
your start with columns you get columns. And someday I will figure out  
how this generalizes to arrays.

apply(mtx, 1, I) #transposes
apply(mtx, 2, I)  #does not "transpose"

-- 
David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list