Skip to contents

Call mode_frequency() to get the number of times that a vector's mode appears in the vector.

See mode_frequency_range() for bounds on an unknown frequency.

Usage

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

Arguments

x

A vector to check for its modal frequency.

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 (length 1) or NA.

Details

By default (na.rm = FALSE), the function returns NA if any values are missing. That is because missings make the frequency uncertain even if the mode is known: any missing value may or may not be the mode, and hence count towards the modal frequency.

See also

mode_first(), which the function wraps.

Examples

# The mode, `9`, appears three times:
mode_frequency(c(7, 8, 8, 9, 9, 9))
#> [1] 3

# With missing values, the frequency
# is unknown, even if the mode isn't:
mode_frequency(c(1, 1, NA))
#> [1] NA

# You can ignore this problem and
# determine the frequency among known values
# (there should be good reasons for this!):
mode_frequency(c(1, 1, NA), na.rm = TRUE)
#> [1] 2