Skip to contents

These functions are vectorized versions of which.max and which.min, which return the index of the value that is maximum or minimum (or the first maximum/minimum value, if there is a tie). In this case, the function is supplied two or more vectors of the same length. For each element at the same position (e.g., the first element in each vector, then the second element, etc.) the function returns an integer indicating which vector has the highest or lowest value (or the index of the first vector with the highest or lowest value in case of ties).

Usage

which.pmax(..., na.rm = TRUE)

which.pmin(..., na.rm = TRUE)

Arguments

...

Two or more vectors. If lengths do not match, the results will likely be be unanticipated.

na.rm

Logical, if FALSE and any of the vectors contains an NA or NaN, the function will return an NA. If TRUE (default), then NA will only be returned if all elements at that position are NA or NaN.

Value

Vector the same length as the input, with numeric values indicating which vector has the highest value at that position. In case of ties, the index of the first vector is returned.

Functions

  • which.pmin(): Which vector has minimum value at each element

Examples

set.seed(123)
a <- sample(9, 5)
b <- sample(9, 5)
c <- sample(9, 5)
a[2:3] <- NA
b[3] <- NA
a[6] <- NA
b[6] <- NA
c[6] <- NA
which.pmax(a, b, c)
#> [1]  2  2  3  2  1 NA
which.pmin(a, b, c)
#> [1]  1  2  3  3  2 NA
which.pmax(a, b, c, na.rm=FALSE)
#> [1]  2 NA NA  2  1 NA
which.pmin(a, b, c, na.rm=FALSE)
#> [1]  1 NA NA  3  2 NA