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 |
If TRUE, |
labels |
A vector of labels for the resulting factor levels. |
label_type |
If |
... |
Other arguments passed to |
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")