[R] More efficient matrix computation

Uwe Ligges ligges at statistik.uni-dortmund.de
Tue Aug 31 18:28:36 CEST 2004


JTW wrote:

> I have a 20x3 matrix as follows:
> 
> 
>>m <- replicate(3, matrix(rnorm(20),20,1))

Why not

   m <- matrix(rnorm(60), 20, 3)


> 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))

That's what apply() is made for:

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

Uwe Ligges



> 
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html




More information about the R-help mailing list