How to find the statistical mode?

前端 未结 30 1634
时光取名叫无心
时光取名叫无心 2020-11-21 07:00

In R, mean() and median() are standard functions which do what you\'d expect. mode() tells you the internal storage mode of the objec

30条回答
  •  说谎
    说谎 (楼主)
    2020-11-21 07:46

    While I like Ken Williams simple function, I would like to retrieve the multiple modes if they exist. With that in mind, I use the following function which returns a list of the modes if multiple or the single.

    rmode <- function(x) {
      x <- sort(x)  
      u <- unique(x)
      y <- lapply(u, function(y) length(x[x==y]))
      u[which( unlist(y) == max(unlist(y)) )]
    } 
    

提交回复
热议问题