cutf2: Cut a Numeric Variable into Intervals

cutf2R Documentation

Cut a Numeric Variable into Intervals

Description

This is a copy of Frank Harell's Hmisc::cut2 function which uses the additional parameters format_fun and ... to apply custom formatting to the input. By default it behaves exactly like Hmisc::cut2

Usage

cutf2(
  x,
  cuts,
  m = 150,
  g,
  levels.mean = FALSE,
  digits,
  minmax = TRUE,
  oneval = TRUE,
  onlycuts = FALSE,
  format_fun = format,
  ...
)

Arguments

x

numeric vector to classify into intervals

cuts

cut points

m

desired minimum number of observations in a group. The algorithm does not guarantee that all groups will have at least m observations.

g

number of quantile groups

levels.mean

set to TRUE to make the new categorical vector have levels attribute that is the group means of x instead of interval endpoint labels

digits

number of significant digits to use in constructing levels. Default is 3 (5 if levels.mean=TRUE)

minmax

if cuts is specified but min(x)<min(cuts) or max(x)>max(cuts), augments cuts to include min and max x

oneval

if an interval contains only one unique value, the interval will be labeled with the formatted version of that value instead of the interval endpoints, ⁠unless oneval=FALSE⁠

onlycuts

set to TRUE to only return the vector of computed cuts. This consists of the interior values plus outer ranges.

format_fun

formatting function

...

additional arguments passed to format_fun

Value

a factor variable with levels of the form ⁠[a,b)⁠ or formatted means (character strings) unless onlycuts is TRUE in which case a numeric vector is returned

Examples

set.seed(1)
Z <- 1000*stats::rnorm(10000)
table(cutf2(Z, c(-10005, -5000, 100, 0, 50, 10000)))
table(cutf2(Z, c(-10005, -5000, 100, 0, 50, 10000), format_fun = formatC))
table(cutf2(Z, c(-10005, -5000, 100, 0, 50, 10000), format_fun = format_metric))
table(cutf2(Z, g=10))      # quantile groups
table(cutf2(Z, g=10, format_fun = formatC))
table(cutf2(Z, g=10, format_fun = format_metric))
table(cutf2(Z, m=500))      # group x into intevals with at least 50 obs.
table(cutf2(Z, m=500, format_fun = formatC))
table(cutf2(Z, m=500, format_fun = format_metric))

moodymudskipper/cutr documentation built on June 10, 2025, 2:57 a.m.