cut_with_bin_labels: 'cut' using endpoints as bin labels bin labels

View source: R/cut_with_bin_labels.R

cut_with_bin_labelsR Documentation

cut using endpoints as bin labels bin labels

Description

Use endpoints as bin labels with cut. The only difference between this function and cut, is that this function will label the levels based on the endpoints if labels are specified as either "left" or "right". For more information, see cut

Usage

cut_with_bin_labels(
  x,
  breaks,
  labels = NULL,
  include.lowest = FALSE,
  right = TRUE,
  dig.lab = 3,
  ordered_result = FALSE,
  ...
)

Arguments

x

a numeric vector which is to be converted to a factor by cutting.

breaks

either a numeric vector of two or more unique cut points or a single number (greater than or equal to 2) giving the number of intervals into which 'x' is to be cut.

labels

labels for the levels of the resulting category. By default, labels are constructed using '"(a,b]"' interval notation. If 'labels = FALSE', simple integer codes are returned instead of a factor. If 'labels = "left"' then the labels will be a numeric verctor with the left endpoints. If 'labels = "right"' then the labels will be a numeric verctor with the right endpoints.

include.lowest

logical, indicating if an 'x[i]' equal to the lowest (or highest, for 'right = FALSE') 'breaks' value should be included.

right

logical, indicating if the intervals should be closed on the right (and open on the left) or vice versa. Default is TRUE

dig.lab

integer which is used when labels are not given. It determines the number of digits used in formatting the break numbers.

...

further arguments passed to or from other methods.

order_result

logical: should the result be an ordered factor?

See Also

cut(), ggplot2::cut_interval(), ggplot2::cut_width(), ggplot2::cut_number()

Examples

data(mtcars)
breaks <- seq(from = 0, to = max(mtcars$hp) + 10, by = 10)
data.table(hp = mtcars$hp,
           bin.cut.default = cut(mtcars$hp, breaks = breaks),
           bin.left.endpoint = cut_with_bin_labels(mtcars$hp, breaks = breaks, labels = "left"),
           bin.right.endpoint = cut_with_bin_labels(mtcars$hp, breaks = breaks, labels = "right")
          ) %>% print

#Using `right` == FALSE
data.table(hp = mtcars$hp,
           bin.cut.default = cut(mtcars$hp, breaks = breaks, right = FALSE),
           bin.left.endpoint = cut_with_bin_labels(mtcars$hp, breaks = breaks, labels = "left", right = FALSE),
           bin.right.endpoint = cut_with_bin_labels(mtcars$hp, breaks = breaks, labels = "right", right = FALSE)
          ) %>% print

ChandlerLutz/CLmisc documentation built on Dec. 2, 2022, 12:40 p.m.