[R] find the mode of a dataset
Thomas Lumley
thomas at biostat.washington.edu
Fri Dec 24 17:51:57 CET 1999
On Fri, 24 Dec 1999, Gil Hauer wrote:
> Hi,
>
> 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
> this?
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
More information about the R-help
mailing list