How to find the statistical mode?

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

    A small modification to Ken Williams' answer, adding optional params na.rm and return_multiple.

    Unlike the answers relying on names(), this answer maintains the data type of x in the returned value(s).

    stat_mode <- function(x, return_multiple = TRUE, na.rm = FALSE) {
      if(na.rm){
        x <- na.omit(x)
      }
      ux <- unique(x)
      freq <- tabulate(match(x, ux))
      mode_loc <- if(return_multiple) which(freq==max(freq)) else which.max(freq)
      return(ux[mode_loc])
    }
    

    To show it works with the optional params and maintains data type:

    foo <- c(2L, 2L, 3L, 4L, 4L, 5L, NA, NA)
    bar <- c('mouse','mouse','dog','cat','cat','bird',NA,NA)
    
    str(stat_mode(foo)) # int [1:3] 2 4 NA
    str(stat_mode(bar)) # chr [1:3] "mouse" "cat" NA
    str(stat_mode(bar, na.rm=T)) # chr [1:2] "mouse" "cat"
    str(stat_mode(bar, return_mult=F, na.rm=T)) # chr "mouse"
    

    Thanks to @Frank for simplification.

提交回复
热议问题