cut_p: Cut a continuous variable into given proportions

View source: R/misc.R

cut_pR Documentation

Cut a continuous variable into given proportions

Description

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.

Usage

cut_p(x, p, ties.method = "random", fct_levels = NULL, verbose = TRUE)

Arguments

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?

Details

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.

Value

Factor variable with x cut into length(p) categories in given proportions

Examples

cut_p(iris$Sepal.Length, p = c(.25, .50, .25), fct_levels = c("short", "middling", "long"))

LukasWallrich/timesaveR documentation built on Nov. 29, 2024, 4:47 a.m.