which.min2: Return Index of (First) Minimum of a Vector

Description Usage Arguments Value Examples

View source: R/which_min2.R

Description

Returns index of minimum 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.min for matrices and for large vectors.

Usage

1
which.min2(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.min2 vs. which.min for integer vector
x <- rpois(10000, lambda = 10)
all.equal(which.min(x), which.min2(x, integer = TRUE))
benchmark(which.min(x), which.min2(x, integer = TRUE), replications = 10000)

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

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