safe_max: Maximum (safe)

View source: R/pal.gen.R

safe_maxR Documentation

Maximum (safe)

Description

Modified version of base::max() that differs in the following ways:

  • NAs in the input (...) are ignored by default (rm_na = TRUE).

  • If the input is of length zero, the output will also be of length zero (of the same type as the input).

  • It is ensured that all inputs are either numeric, of length zero or NA. The only case where the return value will be -Inf or NA is when the input contains only -Inf or NA.

  • Dynamic dots are supported.

Usage

safe_max(..., rm_na = TRUE)

Arguments

...

Numeric objects of which to determine the maximum. Dynamic dots are supported.

rm_na

Ignore missing values in .... If missing values are present and rm_na = FALSE, the result will always be NA.

Value

A numeric scalar or empty value, of the same type as ....

See Also

Other statistical computing / numeric functions: round_to(), safe_min(), safe_seq_len(), stat_mode()

Examples

# other than `base::max()`, this function removes `NA`s by default
max(1, NA_real_, 2)
pal::safe_max(1, NA_real_, 2)

# other than `base::max()`, this function does not return `-Inf` or `NA_character_` for
# zero-length inputs
max(NULL)
max(integer())
pal::safe_max(NULL)
pal::safe_max(integer())

# other than `base::max()`, this function fails for non-numeric inputs
max("zero", 1L)
max("zero", "one")
max(character())
try(pal::safe_max("zero", 1L))
try(pal::safe_max("zero", "one"))
try(pal::safe_max(character()))

salim-b/pal documentation built on Feb. 28, 2025, 6:51 p.m.