[R] how to collapse a list of 1 column matrix to a matrix?

François Pinard pinard at iro.umontreal.ca
Mon Aug 20 05:26:20 CEST 2007


[adschai at optonline.net]
> One more question. After I collapse everything into one matrix, 
> I would like to find the index of column that holds minimum value for 
> each row. I remember that there is a function like maxCols but I can't 
> seem to find the same thing for minimum value.  Any suggestion please?

Here is a possible avenue:

> z <- matrix(sample(1:25), 5)
> z
     [,1] [,2] [,3] [,4] [,5]
[1,]   16    2    9   24    7
[2,]   21   19   22   23   18
[3,]   12    3    5   13   15
[4,]   20    4   25   11   10
[5,]   17    1    8   14    6
> apply(z, 2, which.min)
[1] 3 5 3 4 5

I would presume (yet I did not recently check) that "do.call", 
"which.min", and a flurry of other useful functions, are introduced in 
various R tutorials.  If you plan to use R seriously, it might be worth 
scrutinizing a few of those.

                                        Keep happy!

-- 
François Pinard   http://pinard.progiciels-bpi.ca



More information about the R-help mailing list