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
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 > stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! R-project.org/posting-guide.html
Hi! apply(m,2,quantile,c(0.95,0.99)) /Eryk *********** REPLY SEPARATOR *********** On 8/31/2004 at 9:16 AM 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 >>> >>>______________________________________________ >>>R-help at stat.math.ethz.ch mailing list >>>stat.ethz.ch/mailman/listinfo/r-help >>>PLEASE do read the posting guide! R-project.org/posting-guide.htmlDipl. bio-chem. Witold Eryk Wolski @ MPI-Moleculare Genetic Ihnestrasse 63-73 14195 Berlin 'v' tel: 0049-30-83875219 / \ mail: witek96 at users.sourceforge.net ---W-W---- molgen.mpg.de/~wolski wolski at molgen.mpg.de
A million thanks to all for the prompt reply: apply() works! Jack Wang
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