[R] Mode in case of discrete or categorial data

Thomas Lumley tlumley at u.washington.edu
Fri Nov 12 16:44:24 CET 2004


On Fri, 12 Nov 2004, Vito Ricci wrote:
> Mode<-function(x){t<-table(x)
> if (is.numeric(x)) as.numeric(names(t)[t == max(t)])
> else (names(t)[t == max(t)])
> }
>
> Any other improvement and suggestion will welcome.
>

which.max is design for finding the maximum, so
    names(t)[which.max(t)]


 	-thomas




More information about the R-help mailing list