[R] Mode of a histogram

Michael Doube m.doube at imperial.ac.uk
Thu Jan 14 17:32:17 CET 2010


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

-- 
Dr Michael Doube  BPhil BVSc PhD MRCVS
Research Associate
Department of Bioengineering
Imperial College London
South Kensington Campus
London  SW7 2AZ
United Kingdom



More information about the R-help mailing list