Learn R Programming

tidyna (version 0.4.0)

extrema-functions: NA-aware Extrema Functions

Description

Drop-in replacements for min(), max(), range(), pmax(), and pmin() that default to na.rm = TRUE.

Usage

min(..., na.rm = TRUE, all_na = NULL)

max(..., na.rm = TRUE, all_na = NULL)

range(..., na.rm = TRUE, all_na = NULL, finite = FALSE)

pmax(..., na.rm = TRUE, all_na = NULL)

pmin(..., na.rm = TRUE, all_na = NULL)

Value

For min() and max(), a length-one vector. For range(), a length-two vector containing the minimum and maximum. For pmax() and pmin(), a vector of length equal to the longest input.

Arguments

...

Numeric or character arguments.

na.rm

Logical. Should missing values be removed? Default TRUE.

all_na

Character. What to do when all values are NA: "error" (default) throws an error, "base" returns what base R does with na.rm = TRUE (e.g., Inf for min(), -Inf for max()), "na" returns NA. If NULL, uses getOption("tidyna.all_na", "error").

finite

Logical. If TRUE, removes all non-finite values (NA, NaN, Inf, -Inf). Only applies to range(). Default FALSE.

Examples

Run this code
x <- c(1, NA, 5, 3)
min(x)
max(x)
range(x)

# Multiple arguments
min(c(5, NA), c(1, 2))

# Parallel max/min
pmax(c(1, 5, 3), c(2, 1, 4))
pmin(c(1, NA, 3), c(NA, NA, 1))

# range with infinite values
y <- c(1, Inf, 3, -Inf)
range(y)
range(y, finite = TRUE)

Run the code above in your browser using DataLab