cut_p | R Documentation |
cut()
and similar functions can cut continuous variables by quantile;
other helper functions exist to cut variables into groups of the same size
or width. This function cuts a continuous variable into given proportions.
cut_p(x, p, ties.method = "random", fct_levels = NULL, verbose = TRUE)
x |
A numeric variable that is to be cut into categories |
p |
The proportion of cases to be allocated to each category, in ascending order. Should add up to one, otherwise, it will be scaled accordingly |
ties.method |
Currently accepts only "random" - could be expanded in the future, though it is unclear what a better method would be |
fct_levels |
Character vector with names for levels. If it is NULL, the groups will be labeled with their number and the cut-points employed. |
verbose |
Should boundaries of groups be reported as message? |
Ties within the continuous variable are allocated randomly - so this function should not be used if there are many ties. The number of observations per group is rounded up for even-numbered levels (second, fourth, etc) and rounded down for others (expect for the last level that is used to balance). For large numbers of observations, the distribution will be very close to what is desired, for very small numbers of observations, it should be checked.
Factor variable with x cut into length(p) categories in given proportions
cut_p(iris$Sepal.Length, p = c(.25, .50, .25), fct_levels = c("short", "middling", "long"))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.