get_mode: Find the mode

View source: R/stats.R

get_modeR Documentation

Find the mode

Description

This utility calculates the mode (i.e. the most frequently observed value) for a vector of values. The function function includes an option (see "ties" argument) to return more than one value if there are ties when calculating the mode.

Usage

get_mode(x, ties = TRUE)

Arguments

x

Vector for which the mode should be calculated

ties

Boolean indicating whether or not ties for mode should be returned; if FALSE the function will use which.max to compute the mode and given ties only the first index will be returned; default is TRUE

Details

The mode returned is calculated by tabulating the frequency of each element in the input vector. Note that this method allows for non-numeric vectors to be passed to the function. For example, the function would return the "mode" for a character vector as the most frequently observed string.

Value

A vector containing the most frequently observed values in the x vector. If there are ties in the mode and the "ties" argument is set to TRUE, then the length of the returned vector will be greater than 1. If not, then the length of the returned vector will be 1.

Examples


get_mode(c(1,1,4,3,2,2,2,2))
get_mode(c("dog","dog","dog","monkey", "rat"))
get_mode(c(1,1,1,1,4,3,2,2,2,2), ties = TRUE)
get_mode(c(1,1,1,1,4,3,2,2,2,2), ties = FALSE)


vpnagraj/yawp documentation built on March 31, 2022, 9:56 a.m.