[R] extracting the mode of a vector
Gad Abraham
g.abraham at ms.unimelb.edu.au
Mon Apr 23 09:48:04 CEST 2007
Benoît Lété wrote:
> Hello,
>
> I have an elementary question (for which I couldn't find the answer on the
> web or the help): how can I extract the mode (modal score) of a vector?
Assuming that your vector contains only integers:
> v <- sample(1:5, size=20, replace=T)
> v
[1] 1 1 1 1 2 3 5 1 1 5 2 4 1 3 1 1 5 4 1 5
> vt <- table(v)
> as.numeric(names(vt[vt == max(vt)]))
[1] 1
>
Cheers,
Gad
--
Gad Abraham
Department of Mathematics and Statistics
The University of Melbourne
Parkville 3010, Victoria, Australia
email: g.abraham at ms.unimelb.edu.au
web: http://www.ms.unimelb.edu.au/~gabraham
More information about the R-help
mailing list