R/stat_get_mode.R

Defines functions stat_get_mode

Documented in stat_get_mode

#' stat_get_mode
#' 
#' get the mode of a numeric vector
#'
#' @param v a numeric vector
#'
#' @return get the mode (the most frequent value)
#' @export
#' @examples
#' v<- c(NA, 19, 4, 5, 7, 29, 19, 29, 13, 25, 19, 19)
#' stat_get_mode(v)

stat_get_mode <- function(v) {

  # this is dealing with NA
  uniqv <- stats::na.omit(unique(v))

  uniqv[which.max(tabulate(match(v, uniqv)))]

}
maxbre/myRfuns documentation built on April 15, 2022, 5:45 a.m.