Skip to content
Prev 4116 / 398513 Next

find the mode of a dataset

On Fri, 24 Dec 1999, Gil Hauer wrote:

            
If you really want the sample mode of a vector x you can get it with 

  names(sort(-table(x)))[1]

where table(x) creates a table of the frequencies of each value of x,
multipling by -1 and sorting puts the largest frequency first, and
names()[1] extracts the name of the first element, which is the sample
mode (converted to a string).  You may need to then use as.numeric() on
the result if you want a number.

eg
R> x<-rpois(100,4)
R> names(sort(-table(x)))[1]
[1] "3"
R> table(x)
x
 0  1  2  3  4  5  6  7  8  9 10
 2  9 13 21 15 12 15  7  2  2  2



If your data are continuous (even if they have been rounded off to be
discrete)  you might instead want to estimate the density and pick the
mode of that.  Use hist(x) to get a histogram or plot(density(x)) to get a
kernel estimate of the density of x.



Thomas Lumley
Assistant Professor, Biostatistics
University of Washington, Seattle

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