Modified version of base::max()
that differs in the following ways:
NA
s 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
orNA
is when the input contains only-Inf
orNA
.Dynamic dots are supported.
Arguments
- ...
Numeric objects of which to determine the maximum. Dynamic dots are supported.
- rm_na
Ignore missing values in
...
. If missing values are present andrm_na = FALSE
, the result will always beNA
.
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)
#> [1] NA
pal::safe_max(1, NA_real_, 2)
#> [1] 2
# other than `base::max()`, this function does not return `-Inf` or `NA_character_` for
# zero-length inputs
max(NULL)
#> Warning: no non-missing arguments to max; returning -Inf
#> [1] -Inf
max(integer())
#> Warning: no non-missing arguments to max; returning -Inf
#> [1] -Inf
pal::safe_max(NULL)
#> NULL
pal::safe_max(integer())
#> integer(0)
# other than `base::max()`, this function fails for non-numeric inputs
max("zero", 1L)
#> [1] "zero"
max("zero", "one")
#> [1] "zero"
max(character())
#> Warning: no non-missing arguments, returning NA
#> [1] NA
try(pal::safe_max("zero", 1L))
#> Error in purrr::map(input, function(x) checkmate::assert_numeric(x, typed.missing = TRUE, :
#> ℹ In index: 1.
#> Caused by error in `.f()`:
#> ! Assertion on '...' failed: Must be of type 'numeric' (or 'NULL'), not 'character'.
try(pal::safe_max("zero", "one"))
#> Error in purrr::map(input, function(x) checkmate::assert_numeric(x, typed.missing = TRUE, :
#> ℹ In index: 1.
#> Caused by error in `.f()`:
#> ! Assertion on '...' failed: Must be of type 'numeric' (or 'NULL'), not 'character'.
try(pal::safe_max(character()))
#> Error in purrr::map(input, function(x) checkmate::assert_numeric(x, typed.missing = TRUE, :
#> ℹ In index: 1.
#> Caused by error in `.f()`:
#> ! Assertion on '...' failed: Must be of type 'numeric' (or 'NULL'), not 'character'.