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).
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.
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