Revision 04bbc417cf2927b827660bc07743429783569aec authored by HwB on 10 February 2013, 00:00:00 UTC, committed by Gabor Csardi on 10 February 2013, 00:00:00 UTC
1 parent 0e3ae6b
Raw File
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