R/find_mode.R

Defines functions check_logical check_input find_mode

Documented in find_mode

#' @title Find Mode
#' @description Returns the mode of a given series.
#' @param vector A file path to a local csv file.
#' @param remove_na Whether to remove missing values from the sequence prior to finding the mode. Default is true.
#' @return The most frequent value in the sequence.
#' @export

find_mode <- function(vector, remove_na = TRUE) {
  check_input(vector)
  check_logical(remove_na)
  if(remove_na){
    freq <- table(na.omit(vector))
    mode <- names(freq)[which.max(freq)]
  }
  else {
    freq <- table(vector)
    mode <- names(freq)[which.max(freq)]
  }
  mode
}


check_input <- function(input){
  if(is.null(input)){ #Check that the input is not empty
    stop("\n'vector' must not be null")
  }
  TRUE
}

check_logical <- function(input){
  if(!is.logical(input)){
    stop("\n'remove_na' must be logical")
  }
  TRUE
}
matthiasronnau/matthias documentation built on June 15, 2022, 11:44 p.m.