How to find the statistical mode?

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

    Here is a function to find the mode:

    mode <- function(x) {
      unique_val <- unique(x)
      counts <- vector()
      for (i in 1:length(unique_val)) {
        counts[i] <- length(which(x==unique_val[i]))
      }
      position <- c(which(counts==max(counts)))
      if (mean(counts)==max(counts)) 
        mode_x <- 'Mode does not exist'
      else 
        mode_x <- unique_val[position]
      return(mode_x)
    }
    

提交回复
热议问题