Learn R Programming

bsearchtools (version 0.0.59)

lb: Binary search based lower bound operation

Description

Returns the index pointing to the first element in the vector that is not less than (i.e. greater or equal to) valueToSearch. The behavior is the same as C++ std::lower_bound function, hence, if the vector is empty or valueToSearch is lower than the first element of the vector, it returns the first index (i.e. 1).

The functions suffixed with the vector type (lbNumeric,lbLogical etc.) can be used ONLY with the specified type, otherwise the vector is coerced, and they are (hopefully negligibly) faster then the generic lb function.

For information about NAs handling see details section.

Usage

lb(sortedValues, valueToSearch) lbInteger(sortedValues, valueToSearch) lbNumeric(sortedValues, valueToSearch) lbLogical(sortedValues, valueToSearch) lbCharacter(sortedValues, valueToSearch)

Arguments

sortedValues
A sorted atomic vector of type numeric, integer, logical or character.
valueToSearch
The value to search. If equal to NA, 1 is returned.

Value

The index pointing to the first element in the vector that is not less than (i.e. greater or equal to) valueToSearch.

Details

lb* functions expect sortedValues to be a vector sorted ascending (duplicated values are allowed). Since the binary search functions rely on values comparison (using < operator) and NA cannot be compared by definition, if sortedValues vector contains NA, the result is unpredictable and NO warning is given. Hence remove them before calling these functions.

References

See http://en.cppreference.com/w/cpp/algorithm/lower_bound

Examples

Run this code
lb(c(1,4,5,5,7,9),5) # returns 3
lb(c(1,4,5,5,7,9),-1) # returns 1
lb(numeric(),-1) # returns 1

Run the code above in your browser using DataLab