create_qgroups {dvmisc} | R Documentation |
Create Quantile Groups
Description
Combines quantile
and cut
into a
single function, with strata-specific quantiles possible. For example, you
could create sex-specific height tertiles with
create_qgroups(height, groups = 3, strata = sex)
. Compatible with
dplyr functions like mutate
and
transmute
.
Usage
create_qgroups(x, groups = 4, probs = seq(1/groups, 1 - 1/groups,
1/groups), strata = NULL, quantile_list = list(na.rm = TRUE),
cut_list = list(include.lowest = TRUE))
Arguments
x |
Numeric vector. |
groups |
Numeric value, e.g. 3 for tertiles, 4 for quartiles, etc. |
probs |
Numeric vector. |
strata |
Factor specifying subgroups to calculate quantiles within. For
multivariable subgroups, you can use |
quantile_list |
Arguments to pass to |
cut_list |
Arguments to pass to |
Value
Factor variable.
Examples
# In mtcars dataset, create tertiles for mpg
mtcars$mpg_tertiles <- create_qgroups(mtcars$mpg, groups = 3)
table(mtcars$mpg_tertiles)
# Define tertile cutpoints separately for 4-, 6-, and 8-cylinder vehicles
mtcars$mpg_tertiles <- create_qgroups(mtcars$mpg, groups = 3, strata = mtcars$cyl)
table(mtcars$mpg_tertiles)
# Works with dplyr functions like mutate
mtcars <- mtcars %>%
dplyr::mutate(mpg_tertiles = create_qgroups(mpg, groups = 3, strata = cyl))
table(mtcars$mpg_tertiles)
# Can embed in lm, glm, etc.
summary(lm(mpg ~ create_qgroups(wt), data = mtcars))
[Package dvmisc version 1.1.4 Index]