Skip to contents

Thin R wrapper around sample_mode_cpp(), which returns the most frequently occurring value in data. Integer, logical, double, character, and factor vectors are all supported (dispatched internally on TYPEOF(data); factors preserve their class/levels attributes on the returned value). NA (and, for doubles, NaN as a category distinct from NA) is counted like any other value and can itself be returned as "the mode" if it is the most frequent entry. Ties are broken by first occurrence: among values tied for the highest count, the one that appears earliest in data is returned — this is a positional, not a numeric/lexicographic, tie-break rule.

Usage

sample_mode(data)

Arguments

data

A vector (integer, logical, double, character, or factor) to compute the mode of.

Value

A length-1 vector (same type as data) holding the most frequently occurring value, with ties broken in favor of whichever tied value occurs earliest in data.

Examples

sample_mode(c(1, 2, 2, 3))
#> [1] 2