Skip to content

Mode of a histogram

2 messages · Michael Doube, Dieter Menne

#
Hi all

I recently Googled to find out how to get the mode from my continuous 
data in R, and came up with this ancient thread from the archives:
http://tolstoy.newcastle.edu.au/R/help/99b/1319.html

But sorting is very inefficient if you have lots of data, so can I 
propose this method:

h <- hist(x, breaks=512)
mode <- h$mids[h$counts == max(h$counts)]

of course, breaks can be whatever is sensible for your data, which you 
can check by running

plot(h)

Apologies if this is ridiculously obvious,

Michael
#
Michael Doube-3 wrote:
Mmm. Good to know that sorting is very inefficient. 

Dieter