How to find the statistical mode?

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

    The following function comes in three forms:

    method = "mode" [default]: calculates the mode for a unimodal vector, else returns an NA
    method = "nmodes": calculates the number of modes in the vector
    method = "modes": lists all the modes for a unimodal or polymodal vector

    modeav <- function (x, method = "mode", na.rm = FALSE)
    {
      x <- unlist(x)
      if (na.rm)
        x <- x[!is.na(x)]
      u <- unique(x)
      n <- length(u)
      #get frequencies of each of the unique values in the vector
      frequencies <- rep(0, n)
      for (i in seq_len(n)) {
        if (is.na(u[i])) {
          frequencies[i] <- sum(is.na(x))
        }
        else {
          frequencies[i] <- sum(x == u[i], na.rm = TRUE)
        }
      }
      #mode if a unimodal vector, else NA
      if (method == "mode" | is.na(method) | method == "")
      {return(ifelse(length(frequencies[frequencies==max(frequencies)])>1,NA,u[which.max(frequencies)]))}
      #number of modes
      if(method == "nmode" | method == "nmodes")
      {return(length(frequencies[frequencies==max(frequencies)]))}
      #list of all modes
      if (method == "modes" | method == "modevalues")
      {return(u[which(frequencies==max(frequencies), arr.ind = FALSE, useNames = FALSE)])}  
      #error trap the method
      warning("Warning: method not recognised.  Valid methods are 'mode' [default], 'nmodes' and 'modes'")
      return()
    }
    

提交回复
热议问题