https://github.com/cran/pracma
Raw File
Tip revision: 708a2ad382a163d1eef5af0665e3ae2aad200ced authored by HwB on 21 March 2013, 00:00:00 UTC
version 1.4.5
Tip revision: 708a2ad
mode.R
##
##  m o d e . R
##


Mode <- function(x) {
    if (is.matrix(x))
        x <- c(x)

    if (is.numeric(x)) {
        x   <- sort(x)
        tbl <- table(x)
        n   <- which.max(tbl)
        xm  <- as.numeric(names(tbl)[n])
    } else if (is.complex(x)) {
        x   <- x[order(abs(x))]
        tbl <- table(x)
        n   <- which.max(tbl)
        xm  <- as.complex(names(tbl)[n])
    } else if (is.factor(x)) {
        tbl <- table(x)
        n   <- which.max(tbl)
        xm  <- names(tbl)[n]        
    } else
        xm <- NA

    return(xm)
}
back to top