Learn R Programming

basecamb (version 1.1.2)

quantile_group: Stratify a numeric vector into quantile groups

Description

Transforms a numeric vector into quantile groups. For each input value, the output value corresponds to the quantile that value is in. When grouping into n quantiles, the lowest 1/n of values are assigned 1, the highest 1/n are assigned n.

Usage

quantile_group(data, n, na.rm = TRUE)

Value

vector of length length(data) with the quantile groups

Arguments

data

a vector of type numeric with values to be grouped into quantiles

n

integer indicating number of quantiles, minimum of 2. Must be smaller than length(data)

na.rm

logical; if TRUE all NA values will be removed before calculating groups, if FALSE no NA values are permitted.

Author

J. Peter Marquardt

Details

Tied values will be assigned to the lower quantile group rather than etsimating a distribution. In extreme cases this can mean one or more quantile groups are not represented.

If uneven group sizes cannot be avoided, values will be assigned the higher quantile group.

Examples

Run this code
quantile_group(10:1, 3)
quantile_group(c(rep(1,3), 10:1, NA), 5)

Run the code above in your browser using DataLab