r - How to find the statistical mode?

ID : 5392

viewed : 120

Tags : rstatisticsr-faqr





Top 5 Answer for r - How to find the statistical mode?

vote vote

90

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

85

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

vote vote

75

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

64

I found Ken Williams post above to be great, I added a few lines to account for NA values and made it a function for ease.

Mode <- function(x, na.rm = FALSE) {   if(na.rm){     x = x[!is.na(x)]   }    ux <- unique(x)   return(ux[which.max(tabulate(match(x, ux)))]) } 
vote vote

59

A quick and dirty way of estimating the mode of a vector of numbers you believe come from a continous univariate distribution (e.g. a normal distribution) is defining and using the following function:

estimate_mode <- function(x) {   d <- density(x)   d$x[which.max(d$y)] } 

Then to get the mode estimate:

x <- c(5.8, 5.6, 6.2, 4.1, 4.9, 2.4, 3.9, 1.8, 5.7, 3.2) estimate_mode(x) ## 5.439788 

Top 3 video Explaining r - How to find the statistical mode?







Related QUESTION?