Skip to contents

median_bounds() computes the minimal and maximal possible median values. This is helpful if median2() returns NA: the median can't be determined, but at least it might have lower and upper bounds.

It is used within median_table() to compute the lower and upper columns.

Usage

median_bounds(x, na.rm.amount = 0, even = c("mean", "low", "high"), nna = NULL)

# Default S3 method
median_bounds(x, na.rm.amount = 0, even = c("mean", "low", "high"), nna = NULL)

Arguments

x

Vector that can be ordered using sort(). It will be searched for its possible medians.

na.rm.amount, even

Passed on to median2().

nna

Integer. Ignore unless the function is used as a helper. It is like needs_prep in median_count_tolerable() except it can submit the number of missing values.

Value

Vector of length 2. Its type is double if x is numeric (double or integer). Otherwise, it has the same type as x.

Details

Two edge cases may occur:

  • If the median can be precisely determined, median_bounds() returns two identical values. This is the same case in which median2() would return a non-NA value, and median_plot_errorbar() would show a "ring of certainty".

  • If the number of missing values is so high that a continuous array of them could extend from the start or end of x into the median position, the data do not constrict the median to fall in between any finite bounds. The function will then return c(NA, NA) (of the appropriate type) because such an array of NAs would act as a tunnel to negative or positive infinity, enabling the median to assume indefinitely low or high values.

In the second case, c(NA, NA) means there are no bounds, not that existing bounds are unknown. This is unfortunate but necessary: although c(-Inf, Inf) would be more appropriate conceptually, it is always of type double, so it would lead to coercion bugs when combined with non-numeric data, which is possible in median_table(). To illustrate, try c("abc", Inf).

Like median2(), this function is generic, so methods can be defined for other classes. This documentation describes the default method.

Author

Lukas Jung, R Core Team

Examples

# Lower and upper bounds can be found,
# even though the precise median is unknown:
median_bounds(c(7, 7, 8, 9, NA))
#> [1] 7 8
median_bounds(c(7, 7, 7, 8, 9, 9, NA, NA))
#> [1] 7.0 8.5

# Too many missing values, so there is no finite range:
median_bounds(c(7, 7, 8, 9, NA, NA, NA, NA))
#> [1] NA NA