Mode in case of discrete or categorial data
From: Thomas Lumley 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)]
If you only care about one, which.max() is great. However, if you want to know about all possible ones, which.max() is not the tool:
x <- rep(1:5, c(5, 2, 1, 5, 1)) table(x)
x 1 2 3 4 5 5 2 1 5 1
tab <- table(x) tab[tab == max(tab)]
x 1 4 5 5
tab[which.max(tab)]
1 5 Andy
-thomas
______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html