hutilscpp (version 0.1.0)

range_rcpp: Range C++

Description

Range of a vector using Rcpp.

Usage

range_rcpp(x, warn_empty = TRUE, integer0_range_is_integer = FALSE)

Arguments

x

A vector for which the range is desired. Vectors with missing values are not supported and have no definite behaviour.

warn_empty

(logical, default: TRUE) If x is empty (i.e. has no length), should a warning be emitted (like range)?

integer0_range_is_integer

(logical, default: FALSE) If x is a length-zero integer, should the result also be an integer? Set to FALSE by default in order to be compatible with range, but can be set to TRUE if an integer result is desired, in which case range_rcpp(integer()) is (INT_MAX, -INT_MAX).

Value

A length-4 vector, the first two positions give the range and the next two give the positions in x where the max and min occurred.

This is almost equivalent to c(range(x), which.min(x), which.max(x)). Note that the type is not strictly preserved, but no loss should occur. In particular, logical x results in an integer result, and a double x will have double values for which.min(x) and which.max(x).

A completely empty, logical x returns c(NA, NA, NA, NA) as an integer vector.

Examples

Run this code
# NOT RUN {
x <- rnorm(1e3) # Not noticeable at this scale
bench_system_time(range_rcpp(x))
bench_system_time(range(x))



# }

Run the code above in your browser using DataLab