[R] More efficient matrix computation

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Tue Aug 31 19:02:38 CEST 2004



JTW wrote:

> I have a 20x3 matrix as follows:
> 
> 
>>m <- replicate(3, matrix(rnorm(20),20,1))
> 
> 
> I need to compute, say, 95th and 99th percentiles of
> each column such that the resulting matrix becomes 2x3
> with each row representing the respective percentile. 
> My "best effort" is to compute one column at a time as
> follows:
> 
> 
>>quantile(m[,1], c(0.95, 0.99))
> 
> 
> To do the same for columns 2 and 3, I would simply
> change the column number accordingly.  Clearly, this
> is not very efficient as I may have a large matrix
> (e.g., 100,000x500) to work with.  Any help with the
> code is appreciated.
> 
> Jack Wang
> 

How about:

apply(m, 2, quantile, c(0.95, 0.99))


--sundar




More information about the R-help mailing list