How to find the statistical mode?

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

    Another possible solution:

    Mode <- function(x) {
        if (is.numeric(x)) {
            x_table <- table(x)
            return(as.numeric(names(x_table)[which.max(x_table)]))
        }
    }
    

    Usage:

    set.seed(100)
    v <- sample(x = 1:100, size = 1000000, replace = TRUE)
    system.time(Mode(v))
    

    Output:

       user  system elapsed 
       0.32    0.00    0.31 
    
    0 讨论(0)
  • 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)
    }
    
    0 讨论(0)
  • 2020-11-21 07:46

    This hack should work fine. Gives you the value as well as the count of mode:

    Mode <- function(x){
    a = table(x) # x is a vector
    return(a[which.max(a)])
    }
    
    0 讨论(0)
  • 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)) )]
    } 
    
    0 讨论(0)
  • 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()
    }
    
    0 讨论(0)
  • 2020-11-21 07:47

    This works pretty fine

    > a<-c(1,1,2,2,3,3,4,4,5)
    > names(table(a))[table(a)==max(table(a))]
    
    0 讨论(0)
提交回复
热议问题