[R] statistical mode function

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Aug 24 18:26:56 CEST 2001


On Fri, 24 Aug 2001, Warnes, Gregory R wrote:

>
>
> > 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])

That only finds the rightmost mode if there are several.


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



More information about the R-help mailing list