mode_count_range()
determines the minimal and maximal number
of modes given the number of missing values.
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, setmax_unique
to that number. This rules out thatNA
s represent values beyond that number (see examples). Set it to"known"
instead if no values beyond those already known can occur. Default isNULL
, which assumes no maximum.
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