Skip to contents

mode_count() counts the modes in a vector. Thin wrapper around mode_all().

Usage

mode_count(x, na.rm = FALSE, max_unique = NULL)

Arguments

x

A vector to search for its modes.

na.rm

Logical. Should missing values in x be removed before computation proceeds? Default is FALSE.

max_unique

Numeric or string. If the maximum number of unique values in x is known, set max_unique to that number. This rules out that NAs represent values beyond that number (see examples). Set it to "known" instead if no values beyond those already known can occur. Default is NULL, which assumes no maximum.

Value

Integer. Number of modes (values tied for most frequent) in x. If the modes can't be determined because of missing values, returns NA instead.

Examples

# There are two modes, `3` and `4`:
mode_count(c(1, 2, 3, 3, 4, 4))
#> [1] 2

# Only one mode, `8`:
mode_count(c(8, 8, 9))
#> [1] 1

# Can't determine the number of modes
# here -- `9` might be another mode:
mode_count(c(8, 8, 9, NA))
#> [1] NA

# Either `1` or `2` could be a
# single mode, depending on `NA`:
mode_count(c(1, 1, 2, 2, NA))
#> [1] NA

# `1` is the most frequent value,
# no matter what `NA` stands for:
mode_count(c(1, 1, 1, 2, NA))
#> [1] 1

# Ignore `NA`s with `na.rm = TRUE`
# (there should be good reasons for this!):
mode_count(c(8, 8, 9, NA), na.rm = TRUE)
#> [1] 1
mode_count(c(1, 1, 2, 2, NA), na.rm = TRUE)
#> [1] 2