How to find the statistical mode?

前端 未结 30 1672
时光取名叫无心
时光取名叫无心 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:45

    Could try the following function:

    1. transform numeric values into factor
    2. use summary() to gain the frequency table
    3. return mode the index whose frequency is the largest
    4. transform factor back to numeric even there are more than 1 mode, this function works well!
    mode <- function(x){
      y <- as.factor(x)
      freq <- summary(y)
      mode <- names(freq)[freq[names(freq)] == max(freq)]
      as.numeric(mode)
    }
    

提交回复
热议问题