How to find the statistical mode?

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

    I found Ken Williams post above to be great, I added a few lines to account for NA values and made it a function for ease.

    Mode <- function(x, na.rm = FALSE) {
      if(na.rm){
        x = x[!is.na(x)]
      }
    
      ux <- unique(x)
      return(ux[which.max(tabulate(match(x, ux)))])
    }
    

提交回复
热议问题