Learn R Programming

NCmisc (version 1.2.0)

Mode: 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)

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

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)

Author

Nicholas Cooper njcooper@gmx.co.uk

Examples

Run this code
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

Run the code above in your browser using DataLab