statar (version 0.1.1)

bin: Bin a numeric vector and return integer codes for the binning (corresponds to Stata command xtile)

Description

Bin a numeric vector and return integer codes for the binning (corresponds to Stata command xtile)

Usage

bin(x, cutpoints = NULL, probs = NULL, n_quantiles = NULL, w = NULL)

Arguments

x
A vector
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.
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)
n_quantiles
A numeric specifying number of quantiles. Can be used instead of cutpoints
w
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

Run this code
v <- c(NA, 1:10)
bin(v, n_quantiles = 3) # 3 groups based on terciles
bin(v, probs = c(0.3, 0.7)) # 3 groups based on two quantiles
bin(v, cutpoints = c(2, 3)) # 3 groups based on two cutpoints

Run the code above in your browser using DataCamp Workspace