R/NAME_most.R

Defines functions most

Documented in most

#' Extract the mode
#'
#' \code{most} extracts the most frequent value (mfv; mode) of a numeric vector.
#'
#' This is a convenience function to get the mode of a (numeric) vector.
#'
#'
#' @param x vector (numeric or integer)
#' @return mode most frequent value
#'
#' @examples
#'
#'  most(c(1,1,2))
#'  most(mtcars$cyl)

#' @export
most <- function(x){
  if (!(is.numeric(x))) {
    out <- NA
    return(out)
  }
  x <- na.omit(x)
  t <- table(x)
  m <- max(t)
  out <- as.numeric(names(t)[t==m])
  if (length(out) > 1) out <- out[1]
  if (length(out) == 0) out <- NA
  return(out)
}
sebastiansauer/prada documentation built on Jan. 17, 2024, 11:05 a.m.