Learn R Programming

sortinghat (version 0.1)

which_min: Helper function that determines which element in a vector is the minimum. Ties can be broken randomly or via first/last ordering.

Description

The which_min function is intended to be an alternative to the base which.min function when a specific tie-breaking method is necessary.

Usage

which_min(x, break_ties = c("random", "first", "last"))

Arguments

x
vector
break_ties
method to break ties. The random method selects the index of the minimum elements randomly, while the first and last options imply that the first or last instance of the minimum element will be chosen, respec

Value

  • location of the minimum element in the vector x. If there is a tie, we break the tie with the method specified in break_ties.

Examples

Run this code
set.seed(42)
z <- runif(5)
z <- c(z[1], z[1], z)

which_min(z)
which_min(z, break_ties = "first")
which_min(z, break_ties = "last")

Run the code above in your browser using DataLab