cut_by {clinpubr}R Documentation

Convert Numeric to Factor

Description

Divide numeric data into different groups. Easier to use than base::cut().

Usage

cut_by(
  x,
  breaks,
  breaks_as_quantiles = FALSE,
  labels = NULL,
  label_type = "ori",
  ...
)

Arguments

x

A numeric vector.

breaks

A numeric vector of internal cut points. If breaks_as_quantiles is TRUE, this is a proportion of the data. See Details.

breaks_as_quantiles

If TRUE, breaks is interpreted as a proportion of the data.

labels

A vector of labels for the resulting factor levels.

label_type

If labels is NULL, this sets the label type. "ori" for original labels, "LMH" for "Low Medium High" style. "combined" labels that combine "LMH" type or provided labels with the original range labels.

...

Other arguments passed to base::cut().

Details

cut_by() is a wrapper for base::cut(). Compared with the argument breaks in base::cut(), breaks here automatically sets the minimum and maximum to -Inf and Inf.

Value

A factor.

Note

The argument right in base::cut() is always set to FALSE, which means the levels follow the left closed right open convention.

Examples

set.seed(123)
cut_by(rnorm(100), c(0, 1, 2))
cut_by(rnorm(100), c(1 / 3, 2 / 3), breaks_as_quantiles = TRUE, label_type = "LMH")

[Package clinpubr version 1.0.1 Index]