xtile: Bin variable in groups (similar to Stata xtile)

Description Usage Arguments Value Examples

View source: R/xtile.R

Description

Bin variable in groups (similar to Stata xtile)

Usage

1
xtile(x, n = NULL, probs = NULL, cutpoints = NULL, wt = NULL)

Arguments

x

A vector

n

A numeric specifying number of quantiles. Can be used instead of cutpoints

probs

A vector of probabilities that an be used instead of cutpoints. Quantiles are computed as the inverse of the empirical distribution function (type = 1)

cutpoints

Cutpoints to use when nq is not specified. For instance cutpoints = 0.4 creates two groups, one for observations equal or below 0.4, one for observations superior to 0.4.

wt

A variable specifying weight in case the option n_quantiles is specified.

Value

An integer vector representing groups corresponding to cutpoints. Includes missing values when present in the original vector.

Examples

1
2
3
4
x <- c(NA, 1:10)                   
xtile(x, n = 3) # 3 groups based on terciles
xtile(x, probs = c(0.3, 0.7)) # 3 groups based on two quantiles
xtile(x, cutpoints = c(2, 3)) # 3 groups based on two cutpoints

Example output

 [1] NA  1  1  1  1  2  2  2  3  3  3
 [1] NA  1  1  1  2  2  2  2  3  3  3
 [1] NA  1  1  2  3  3  3  3  3  3  3

statar documentation built on Jan. 13, 2021, 9:33 p.m.