R/Mode.R

#' @title Calculate the Mode of a given Dataset
#' 
#' @description Calculates the mode of a given dataset.
#' 
#' @param x A dataset from which the categorical variables are extracted
#' 
#' @param na.rm A logical object indicating how to deal with NA values.
#' 
#' @return Outputs the categorical variables as a data frame.
#' 
#' @export
#'
#' @seealso 
#' 
#' @references Gregor, jprockbelly and Williams, K. (2012). Is there a built-in function for finding the mode? Retrieved from https://stackoverflow.com/questions/2547402/is-there-a-built-in-function-for-finding-the-mode
#'
#' @examples 
#' # Example Data
#' 
Mode <- function(x, 
                 na.rm = FALSE) 
  {
  
  if(na.rm == TRUE){
    
    x = x[!is.na(x)]
    
  }
  
  ux <- unique(x)
  
  return(ux[which.max(tabulate(match(x, ux)))])
  
}
oislen/BuenaVista documentation built on May 16, 2019, 8:12 p.m.