How to find the statistical mode?

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

    R has so many add-on packages that some of them may well provide the [statistical] mode of a numeric list/series/vector.

    However the standard library of R itself doesn't seem to have such a built-in method! One way to work around this is to use some construct like the following (and to turn this to a function if you use often...):

    mySamples <- c(19, 4, 5, 7, 29, 19, 29, 13, 25, 19)
    tabSmpl<-tabulate(mySamples)
    SmplMode<-which(tabSmpl== max(tabSmpl))
    if(sum(tabSmpl == max(tabSmpl))>1) SmplMode<-NA
    > SmplMode
    [1] 19
    

    For bigger sample list, one should consider using a temporary variable for the max(tabSmpl) value (I don't know that R would automatically optimize this)

    Reference: see "How about median and mode?" in this KickStarting R lesson
    This seems to confirm that (at least as of the writing of this lesson) there isn't a mode function in R (well... mode() as you found out is used for asserting the type of variables).

提交回复
热议问题