Mode: Find the mode of a vector.

View source: R/NCmisc.R

ModeR Documentation

Find the mode of a vector.

Description

The mode is the most common value in a series. This function can return multiple values if there are equally most frequent values, and can also work with non-numeric types.

Usage

Mode(x, multi = FALSE, warn = FALSE)

Arguments

x

The data to take the mode from. Dimensions and NA's are removed if possible, strings, factors, numeric all permitted

multi

Logical, whether to return multiple modes if values have equal frequency

warn

Logical, whether to give warnings when multiple values are found (if multi=FALSE)

Value

The most frequent value, or sorted set of most frequent values if multi==TRUE and there are more than one. Numeric if x is numeric, else as strings

Author(s)

Nicholas Cooper njcooper@gmx.co.uk

Examples

Mode(c(1,2,3,3,4,4)) # 2 values are most common, as multi=FALSE, 
# selects the last value (after sort)
Mode(c(1,2,3,3,4,4),multi=TRUE) # same test with multi=T, 
# returns both most frequent
Mode(matrix(1:16,ncol=4),warn=TRUE) # takes mode of the entire
# matrix treating as a vector, but all values occur once
Mode(c("Tom","Dick","Harry"),multi=FALSE,warn=TRUE) # selects last
# sorted value, but warns there are multiple modes
Mode(c("Tom","Dick","Harry"),multi=TRUE,warn=TRUE) # multi==TRUE so
# warning is negated

NCmisc documentation built on Oct. 17, 2022, 5:09 p.m.