Learn R Programming

dvmisc (version 1.1.4)

which_max_nv: Return Index of (First) Maximum of a Numeric Vector

Description

Written in C++, this function tends to run faster than which.max for large numeric vectors.

Usage

which_max_nv(x)

Arguments

x

Numeric vector.

Value

Integer value.

Details

For optimal speed, choose the version of this function that matches the class of your x:

which_max_nv for numeric vector. which_max_iv for integer vector. which_max_nm for numeric matrix. which_max_im for integer matrix.

Examples

Run this code
# NOT RUN {
# For long vectors, which_max_nv is faster than which.max
x <- rnorm(100000)
which.max(x) == which_max_nv(x)
benchmark(which.max(x), which_max_nv(x), replications = 500)

# For shorter vectors, which_max_nv is slower than which.max
x <- rnorm(100)
which.max(x) == which_max_nv(x)
benchmark(which.max(x), which_max_nv(x), replications = 10000)

# }

Run the code above in your browser using DataLab