Skip to content
Prev 33687 / 398513 Next

Summary for mode of a data set

Your "mode1" function will identify multiple modes only if they have the 
same number of observations.  Consider the following:

 > x2 <- c(2, 1,1, 3,3,3)
 > mode1(x2)
[1] 3

Here, "mode1" did not identify the local mode at 1, because it had fewer 
observations than 3.  If you want the modes at both 1 and 3, then 
consider the following:

modes <- function(x){
	xt <- table(x)
	nt <- length(xt)
	sel <- c(xt[-nt]>=xt[-1], T)&c(T, xt[-1]>=xt[-nt])
	as.numeric(names(xt[sel]))
}
 > modes(x2)
[1] 1 3

hth.  spencer graves
Erin Hodgess wrote: