View source: R/utils-helpers.R
| cut_quantile | R Documentation |
cut_quantile() bins a numeric vector into n_bins quantile groups.
cut_exposure_quantile() does the same for an exposure variable,
additionally keeping placebo (0) observations in their own bin.
cut_exposure_quantile(
x,
n_bins = 4,
is_placebo = NULL,
ties = c("upward", "downward", "split-even"),
seed = NULL,
quantile_type = 7,
labeller = NULL
)
cut_quantile(
x,
n_bins = 4,
ties = c("upward", "downward", "split-even"),
seed = NULL,
quantile_type = 7,
labeller = NULL
)
x |
Numeric vector |
n_bins |
Number of bins |
is_placebo |
Logical vector indicating placebo samples |
ties |
Rule for assigning a value that sits exactly on an interior
break point, where the bin membership would otherwise be ambiguous.
|
seed |
Optional single number used to seed the random tie-break
used by |
quantile_type |
Integer between 1 and 9, passed straight through
as |
labeller |
Controls the labels used for the |
Both functions error if x has fewer than 2 distinct
non-missing values, since quantile bins aren't well-defined in that
case. If x doesn't have enough resolution to distinguish all n_bins
requested bins (e.g. many repeated values clustered at one end),
both functions warn and fall back to using as many bins as the data
supports, rather than erroring or silently showing fewer bins with
no explanation. cut_exposure_quantile()'s "breaks" attribute is
read back out by quantile-layer builders that draw bin-boundary
separators (e.g. er_style_quantile_errorbar_vlines()) via
attr(exposure_bins, "breaks"). Because that fallback can lower n_bins
below what was originally requested, a character-vector labeller
is length-checked against the actual bin count, not the requested
one, and errors informatively on a mismatch.
A factor with "ties" and "quantile_type" attributes
recording those two arguments. cut_exposure_quantile()'s result
additionally carries a "breaks" attribute holding the n_bins + 1
quantile cutpoints used to form the bins.
x <- rnorm(100)
cut_quantile(x)
cut_exposure_quantile(abs(x))
cut_quantile(x, ties = "split-even", seed = 8213)
cut_quantile(x, quantile_type = 1)
cut_quantile(x, labeller = function(n_bins, breaks) paste0("Group ", 1:n_bins))
cut_quantile(x, labeller = c("Low", "Mid-low", "Mid-high", "High"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.