[R] Summary for mode of a data set
Spencer Graves
spencer.graves at pdf.com
Mon Jun 23 20:53:45 CEST 2003
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:
> Dear R People:
>
> thank you for the many helpful sets of code that I received!!!
>
> I combined several of the concepts for the following function:
>
>
>>mode1
>
>
> function(x) {
>
> y <- rle(sort(x))
>
> z <- y$values[y$lengths==max(y$lengths)]
>
> return(z)
>
> }
>
>
>>xm
>
>
> [1] 22 15 10 30 25 26 2 17 28 2 24 6 26 24 5 22 20 14
>
>
>>mode1(xm)
>
>
> [1] 2 22 24 26
>
>
>
> This will pick up multiple modes.
>
> Again thanks to all who helped!
>
> Sincerely,
> Erin
> mailto: hodgess at uhddx.01.dt.uh.edu
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
More information about the R-help
mailing list