R/mode.R

Defines functions Mode

Documented in Mode

##
##  m o d e . R
##


Mode <- function(x) {
    if (all(is.na(x))) return(NA)
    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)
}

Try the pracma package in your browser

Any scripts or data that you put into this service are public.

pracma documentation built on Nov. 10, 2023, 1:14 a.m.