[R] statistical mode function

Mark Myatt mark at myatt.demon.co.uk
Tue Sep 18 13:26:26 CEST 2001


Warnes, Gregory R <gregory_r_warnes at groton.pfizer.com> writes:

>> From: ben at zoo.ufl.edu [mailto:ben at zoo.ufl.edu]
>>
>[snip]
>>   How about
>> 
>> mode1 <- function(x) {
>>   t0 <- table(x)
>>   as.numeric(names(t0)[t0==max(t0)])
>> }
>> 
>> e.g.
>>  mode1(rbinom(100,10,0.4))
>> 
>[snip]
>>
>>   If someone else can come up with a one-liner I'd be mildly 
>> interested.
>>
>
>How about:
>
>as.numeric(names(rev(sort(table(x))))[1])

That's better than:

        as.numeric(names(table(x)[table(x) == max(table(x))])[1])

which I have been using.

Mark

--
Mark Myatt


-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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