.bincode

0th

Percentile

Bin a Numeric Vector

Bin a numeric vector and return integer codes for the binning.

Keywords
category
Usage
.bincode(x, breaks, right = TRUE, include.lowest = FALSE)
Arguments
x
a numeric vector which is to be converted to integer codes by binning.
breaks
a numeric vector of two or more cut points, sorted in increasing order.
right
logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa.
include.lowest
logical, indicating if an ‘x[i]’ equal to the lowest (or highest, for right = FALSE) ‘breaks’ value should be included in the first (or last) bin.
Details

This is a ‘barebones’ version of cut.default(labels = FALSE) intended for use in other functions which have checked the arguments passed. (Note the different order of the arguments they have in common.)

Unlike cut, the breaks do not need to be unique. An input can only fall into a zero-length interval if it is closed at both ends, so only if include.lowest = TRUE and it is the first (or last for right = FALSE) interval.

Value

An integer vector of the same length as x indicating which bin each element falls into (the leftmost bin being bin 1). NaN and NA elements of x are mapped to NA codes, as are values outside range of breaks.

cut, tabulate
library(base) ## An example with non-unique breaks: x <- c(0, 0.01, 0.5, 0.99, 1) b <- c(0, 0, 1, 1) .bincode(x, b, TRUE) .bincode(x, b, FALSE) .bincode(x, b, TRUE, TRUE) .bincode(x, b, FALSE, TRUE)