[R] statistical mode function

ben@zoo.ufl.edu ben at zoo.ufl.edu
Fri Aug 24 14:36:03 CEST 2001


  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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list