Learn R Programming

cheapr (version 1.1.0)

bin: A sometimes cheaper but argument richer alternative to .bincode()

Description

When x is an integer vector, bin() is cheaper than .bincode() as no coercion to a double vector occurs. This alternative also has more arguments that allow you to return the start values of the binned vector, as well as including out-of-bounds intervals.

Usage

bin(
  x,
  breaks,
  left_closed = TRUE,
  include_endpoint = FALSE,
  include_oob = FALSE,
  codes = TRUE
)

Value

Either an integer vector of codes indicating which bin the values fall into, or the start of the intervals for which each value falls into.

Arguments

x

A numeric vector.

breaks

A numeric vector of breaks.

left_closed

Should intervals be left-closed (and right-open)? Default is TRUE. If FALSE they are left-open (and right-closed).

include_endpoint

Equivalent to include.lowest in ?.bincode.

include_oob

Should out-of-bounds interval be included? Default is FALSE. This is the equivalent of adding Inf as the last value of the breaks, or -Inf as the first value of the breaks if left_closed = FALSE. When TRUE, this essentially becomes findInterval().

codes

Should an integer vector indicating which bin the values fall into be returned? Default is TRUE. If FALSE the start values of the respective bin intervals are returned, i.e the corresponding breaks.

See Also

get_breaks as_discrete