cut-interval

Cut a numeric vector into n equal-width bins.

Splits the closed range [min, max] of values into n bins of equal width. Bins are right-open ((lo, hi]), except the leftmost bin is closed on the left so the minimum value is captured. The default labels are "(lo,hi]" strings using the bin boundaries.

Usage

cut-interval(
  values,
  n: 4,
  labels: auto,
)

Parameters

Parameter Default Description
values Array of numbers; non-numeric or none entries map to none.
n 4 Number of equal-width bins. Must be positive.
labels auto Either auto for default boundary labels, or an array of length n providing explicit labels.

Returns

Array of bin labels with the same length as values.

Examples

Default labels show the boundary intervals.

#let bins = cut-interval((1, 2, 3, 4, 5, 6, 7, 8), n: 4)
// ("(1,2.75]", "(1,2.75]", "(2.75,4.5]", "(2.75,4.5]", ...)

Provide explicit labels per bin for tidier display.

#let bins = cut-interval(
  (1, 2, 3, 4, 5, 6, 7, 8),
  n: 4,
  labels: ("low", "mid-low", "mid-high", "high"),
)
Back to top