R/get_mode.R

Defines functions get_mode

Documented in get_mode

#' Get the mode (most frequent) of a vector
#'
#' If there is a tie, get the first one.
#'
#' @param x a vector
#'
#' @return a scalar
#'
#' @examples
#' get_mode(c(1, 2, 2, 3))
#'
#' @export
get_mode <- function(x) {
    ux <- unique(x)
    ux[which.max(tabulate(match(x, ux)))]
}
yimingli/r-li documentation built on March 31, 2020, 5:45 a.m.