R/mode.R

Defines functions mode

Documented in mode

#' Mode Function
#'
#' This function returns the mode for a given data frame.
#' @param x variable within data frame or a list of values.
#' @param na.rm remove the NAs, default is FALSE.
#' @return This function returns the mode for a variable within a data frame or a list of values.
#' @examples
#'
#' data <- mtcars
#'
#' mode(data$mpg)
#' @export

mode <- function(x, na.rm = FALSE) {
  if(na.rm){
    x = x[!is.na(x)]
  }

  ux <- unique(x)
  return(ux[which.max(tabulate(match(x, ux)))])
}

Try the vannstats package in your browser

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

vannstats documentation built on April 15, 2023, 9:09 a.m.