Learn R Programming

DataSum (version 0.1.1)

getmode: Get Mode of a Vector (numeric/character/factor)

Description

Returns the mode(s) of a vector. By default, returns a single string with all modes collapsed by comma when there are ties. If you need only one mode, set collapse = FALSE to return the first mode deterministically.

Usage

getmode(x, collapse = TRUE)

Value

A single value (first mode) or a comma-separated string of modes.

Arguments

x

A vector (numeric, character, factor, etc.).

collapse

Logical; if TRUE (default), return all modes as a single comma-separated string. If FALSE, return the first mode only.

Examples

Run this code
getmode(c(1, 2, 2, 3, 4))
getmode(c("a", "b", "b", "a"), collapse = TRUE)
getmode(c("a", "b", "b", "a"), collapse = FALSE)

Run the code above in your browser using DataLab