which.max2: Return Index of (First) Maximum of a Vector

Description Usage Arguments Value Examples

View source: R/which_max2.R

Description

Returns index of maximum for vectors and index or (row, column) position for matrices. For optimal speed, use integer = TRUE if x is an integer vector/matrix and integer = FALSE otherwise. Typically faster than which.max for matrices and for large vectors.

Usage

1
which.max2(x, arr.ind = FALSE, integer = FALSE)

Arguments

x

Integer or numeric vector/matrix.

arr.ind

Logical value for whether to return (row, col) position rather than vector position, if x is a matrix.

integer

Logical value for whether x is an integer vector/matrix.

Value

Numeric value.

Examples

1
2
3
4
5
6
7
8
9
# which.max2 vs. which.max for integer vector
x <- rpois(10000, lambda = 5)
all.equal(which.max(x), which.max2(x, integer = TRUE))
benchmark(which.max(x), which.max2(x, integer = TRUE), replications = 10000)

# which.max2 vs. which.max for numeric vector
x <- rnorm(10000)
all.equal(which.max(x), which.max2(x))
benchmark(which.max(x), which.max2(x), replications = 10000)

dvmisc documentation built on Dec. 18, 2019, 1:35 a.m.