Hello R-users, I am aware that this is a very basic question, but still... Since mode() function is reserved for something else, is there a function to calculate statistical mode for a vector or a matrxi? i.e. mode.function(c(1,2,2,2,3,3))=2 Thanks in advance, Elena Moltchanova Elena MOLTCHANOVA IIASA International Institute for Applied Systems Analysis A-2361 Laxenburg, Austria E-Mail: moltchan at iiasa.ac.at Phone : +43 2236 807-0 Fax : +43 2236 71313 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 Fri, 24 Aug 2001, Elena MOLTCHANOVA wrote:> Hello R-users, > > I am aware that this is a very basic question, but still... Since mode() > function is reserved for something else, is there a function to calculate > statistical mode for a vector or a matrxi? > > i.e. mode.function(c(1,2,2,2,3,3))=2If that is to be regarded as a discrete distribution, statmod <- function(x) { z <- table(as.vector(x)) names(z)[z == max(z)] } should do the trick. It gives a character result because the input x represents categories. It also reports all modes.> > Thanks in advance, > > Elena Moltchanova > Elena MOLTCHANOVA > IIASA > International Institute for Applied Systems Analysis > A-2361 Laxenburg, Austria > E-Mail: moltchan at iiasa.ac.at > Phone : +43 2236 807-0 > Fax : +43 2236 71313 > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
How about mode1 <- function(x) { t0 <- table(x) as.numeric(names(t0)[t0==max(t0)]) } e.g. mode1(rbinom(100,10,0.4)) (no bells and whistles, error-checking, etc. etc.) of course, this only works for discrete distributions; I imagine you could do something with density() to get an estimate of the mode for a continuous distribution ... If someone else can come up with a one-liner I'd be mildly interested. Ben Bolker On Fri, 24 Aug 2001, Elena MOLTCHANOVA wrote:> Hello R-users, > > I am aware that this is a very basic question, but still... Since mode() > function is reserved for something else, is there a function to calculate > statistical mode for a vector or a matrxi? > > i.e. mode.function(c(1,2,2,2,3,3))=2 > > Thanks in advance, > > Elena Moltchanova > Elena MOLTCHANOVA > IIASA > International Institute for Applied Systems Analysis > A-2361 Laxenburg, Austria > E-Mail: moltchan at iiasa.ac.at > Phone : +43 2236 807-0 > Fax : +43 2236 71313 > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > 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 > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> From: ben at zoo.ufl.edu [mailto:ben at zoo.ufl.edu] >[snip]> How about > > mode1 <- function(x) { > t0 <- table(x) > as.numeric(names(t0)[t0==max(t0)]) > } > > e.g. > mode1(rbinom(100,10,0.4)) >[snip]> > If someone else can come up with a one-liner I'd be mildly > interested. >How about: as.numeric(names(rev(sort(table(x))))[1]) -Greg LEGAL NOTICE Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately. -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._