How to find the statistical mode?
One more solution, which works for both numeric & character/factor data:
Mode <- function(x) { ux <- unique(x) ux[which.max(tabulate(match(x, ux)))]}
On my dinky little machine, that can generate & find the mode of a 10M-integer vector in about half a second.
If your data set might have multiple modes, the above solution takes the same approach as which.max
, and returns the first-appearing value of the set of modes. To return all modes, use this variant (from @digEmAll in the comments):
Modes <- function(x) { ux <- unique(x) tab <- tabulate(match(x, ux)) ux[tab == max(tab)]}
There is package modeest
which provide estimators of the mode of univariate unimodal (and sometimes multimodal) data and values of the modes of usual probability distributions.
mySamples <- c(19, 4, 5, 7, 29, 19, 29, 13, 25, 19)library(modeest)mlv(mySamples, method = "mfv")Mode (most likely value): 19 Bickel's modal skewness: -0.1 Call: mlv.default(x = mySamples, method = "mfv")
For more information see this page
found this on the r mailing list, hope it's helpful. It is also what I was thinking anyways. You'll want to table() the data, sort and then pick the first name. It's hackish but should work.
names(sort(-table(x)))[1]