Skip to contents

mode_count_range() determines the minimal and maximal number of modes given the number of missing values.

Usage

mode_count_range(x, max_unique = NULL)

Arguments

x

A vector to search for its possible modes.

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 2). Minimal and maximal number of modes (values tied for most frequent) in x.

Details

If x is a factor, max_unique should be "known" or there is a warning. This is because a factor's levels are supposed to include all of its possible values.

Examples

# If `NA` is `7` or `8`, that number is
# the only mode; otherwise, both numbers
# are modes:
mode_count_range(c(7, 7, 8, 8, NA))
#> [1] 1 2

# Same result here -- `7` is the only mode
# unless `NA` is secretly `8`, in which case
# there are two modes:
mode_count_range(c(7, 7, 7, 8, 8, NA))
#> [1] 1 2

# But now, there is no way for `8` to be
# as frequent as `7`:
mode_count_range(c(7, 7, 7, 7, 8, 8, NA))
#> [1] 1 1

# The `NA`s might form a new mode here
# if they are both, e.g., `9`:
mode_count_range(c(7, 7, 8, 8, NA, NA))
#> [1] 1 3

# However, if there can be no values beyond
# those already known -- `7` and `8` --
# the `NA`s can't form a new mode.
# Specify this with `max_unique = "known"`:
mode_count_range(c(7, 7, 8, 8, NA, NA), max_unique = "known")
#> [1] 1 2