[R] find the mode of a dataset
thomas at biostat.washington.edu
Fri Dec 24 17:51:57 CET 1999
On Fri, 24 Dec 1999, Gil Hauer wrote:
> First, a warning: I'm new to this.
> I have a data sample which I am displaying with the hist() command; no
> problems there. I would like to find three other pieces of information: the
> minimum, the maximum and the sample mode (most frequently occurring data
> item). The first two are no problem but the last has me stumped. I tried to
> browse the documentation to no avail. Is there anyone who knows how to do
If you really want the sample mode of a vector x you can get it with
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() 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.
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.
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
More information about the R-help