Hi together, I have some data in a matrix structure - say 1000 rows with 10 columns. And I like to do some calculations (like max, avg or min) on row level. The only solution I found so fare was using a loop like for (i in 1:1000) { max[i] <- max(matrix[I,]) } It looks like that this is not very fast. Does an other way exists? Kind regards Ulrich --------------------------------------------------------- This Mail has been checked for Viruses Attention: Encrypted mails can NOT be checked! ** Diese Mail wurde auf Viren geprueft Hinweis: Verschluesselte mails koennen NICHT auf Viren geprueft werden! --------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"Arndt, Ulrich, VF-VP" <ulrich.arndt at vodafone.de> writes:> Hi together, > > I have some data in a matrix structure - say 1000 rows with 10 columns. And > I like to do some calculations (like max, avg or min) on row level. > The only solution I found so fare was using a loop like > > for (i in 1:1000) { > max[i] <- max(matrix[I,]) > } > > It looks like that this is not very fast. > Does an other way exists?apply(matrix,1,max) -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Mon, 28 Jan 2002, Arndt, Ulrich, VF-VP wrote:> Hi together, > > I have some data in a matrix structure - say 1000 rows with 10 columns. And > I like to do some calculations (like max, avg or min) on row level. > The only solution I found so fare was using a loop like > > for (i in 1:1000) { > max[i] <- max(matrix[I,]) > } > > It looks like that this is not very fast.Only 0.12 secs on my machine. You did set max to numeric(1000) first?> Does an other way exists?More comprehensible, but not necessarily much faster is mymax <- apply(mymatrix, 1, max) (0.03 secs) Try to avoid confusing people (and perhaps R) by calling objects the same name as system ones (max, matrix). For average, rowMeans in R-devel is much faster still, if you need to save another 0.03 seconds .... -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> Hi together, > > I have some data in a matrix structure - say 1000 rows with 10 columns. And > I like to do some calculations (like max, avg or min) on row level. > The only solution I found so fare was using a loop like > > for (i in 1:1000) { > max[i] <- max(matrix[I,]) > }this will not work anyway (`I' is unknown) and it is unwise to overwrite `matrix'. R> apply(mymatrix, 1, max) will do, Torsten> > It looks like that this is not very fast. > Does an other way exists? > > Kind regards > > Ulrich > > > --------------------------------------------------------- > This Mail has been checked for Viruses > Attention: Encrypted mails can NOT be checked! > > ** > > Diese Mail wurde auf Viren geprueft > Hinweis: Verschluesselte mails koennen NICHT auf Viren geprueft werden! > --------------------------------------------------------- > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Arndt, Ulrich, VF-VP <ulrich.arndt at vodafone.de> wrote:> I like to do some calculations (like max, avg or min) on row level.Prof Brian D Ripley <ripley at stats.ox.ac.uk> replied, in part:> For average, rowMeans in R-devel is much faster still, if you need to save > another 0.03 seconds ....If you don't want to install a "devel" version of R and can't wait for 1.5.0, try package "colSums" at: <http://cran.r-project.org/src/contrib/Devel/colSums_1.0.tar.gz> which contains a function rowMeans() (but not rowMax or rowMin, which would be nice additions). By the way, I will be submitting version 1.1 soon, which incorporates the "2-pass" method in colVars and rowVars. -- -- David Brahm (brahm at alum.mit.edu) -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._