> > I have an array, one say ar, with 3 dimensions. > dim(ar) > >200 3 4 > I would like to calculate the mean on the third dimension. > mean(ar[1,1,]),mean(ar[1,2,]) etc..., mean[200,3,]. > > How can i do this ?apply(ar,c(1,2),mean) will produce a (200 X 3) matrix of means averaged over the third dimension. J.R. Lockwood 412-683-2300 x4941 lockwood at rand.org http://www.rand.org/methodology/stat/members/lockwood/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
In general this is what "apply" does. In your example you could use rowMeans(ar) as well, I think. Reid Huntsinger -----Original Message----- From: Olivier Martin [mailto:olivier.martin at inrialpes.fr] Sent: Tuesday, July 02, 2002 10:55 AM To: r-help Subject: [R] mean and array Hi all, I have an array, one say ar, with 3 dimensions. dim(ar) >200 3 4 I would like to calculate the mean on the third dimension. mean(ar[1,1,]),mean(ar[1,2,]) etc..., mean[200,3,]. How can i do this ? Thanks -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- Olivier MARTIN PhD student phone : (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail: olivier.martin at inrialpes.fr 38334 Saint Ismier cedex web : http://www.inrialpes.fr/is2/people/omartin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._ ------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, contains information of Merck & Co., Inc. (Whitehouse Station, New Jersey, USA) that may be confidential, proprietary copyrighted and/or legally privileged, and is intended solely for the use of the individual or entity named on this message. If you are not the intended recipient, and have received this message in error, please immediately return this by e-mail and then delete it. ============================================================================= -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 all, I have an array, one say ar, with 3 dimensions. dim(ar) >200 3 4 I would like to calculate the mean on the third dimension. mean(ar[1,1,]),mean(ar[1,2,]) etc..., mean[200,3,]. How can i do this ? Thanks -- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- Olivier MARTIN PhD student phone : (33) 04 76 61 53 55 Projet IS2 06 08 67 93 42 INRIA Rhone-Alpes fax : (33) 04 76 61 54 77 655, Av. de l'Europe Montbonnot e-mail: olivier.martin at inrialpes.fr 38334 Saint Ismier cedex web : http://www.inrialpes.fr/is2/people/omartin -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Olivier Martin <olivier.martin at inrialpes.fr> wrote:> I have an array, one say ar, with 3 dimensions. > dim(ar) > >200 3 4 > I would like to calculate the mean on the third dimension. > mean(ar[1,1,]),mean(ar[1,2,]) etc..., mean[200,3,].Reid Huntsinger <reid_huntsinger at merck.com> replied:> In your example you could use rowMeans(ar) as well, I think.Specifically: rowMeans(ar, dims=2) This gives the same result as apply(ar, 1:2, mean), but is faster. Note rowMeans() and friends were introduced in R-1.5.0. -- -- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._