loading...

cut_quantile() is like cut(), but it calculates intervals from quantiles such that each interval has approximately the same number of items from the original vector. x must have both quantile() and cut() methods implemented.

cut_quantile(x, breaks, labels = NULL, ...)

Arguments

x

An R object, usually a numeric vector.

breaks

A single integer with the number of breaks to use.

labels

Labels for the resulting category or NULL (by default) to construct them automatically like "(a,b]". If labels = FALSE, simple integer codes are returned instead of factor.

...

Further arguments passed to cut().

Value

A factor() is returned, unless labels = FALSE (in this case, a integer vector is obtained).

Examples

# Transform a numeric vector into a factor with 5 levels of same item numbers
vec <- rnorm(20)
fact <- cut_quantile(vec, breaks = 5)
fact
#>  [1] (-0.261,0.114]  (-0.261,0.114]  (-0.918,-0.261] (-0.918,-0.261]
#>  [5] (0.56,2.07]     (0.56,2.07]     (-1.86,-0.918]  (0.114,0.56]   
#>  [9] (-1.86,-0.918]  (-0.918,-0.261] (-0.261,0.114]  (0.114,0.56]   
#> [13] (-0.918,-0.261] (0.114,0.56]    (0.114,0.56]    (-1.86,-0.918] 
#> [17] (0.56,2.07]     (0.56,2.07]     (-0.261,0.114]  (-1.86,-0.918] 
#> 5 Levels: (-1.86,-0.918] (-0.918,-0.261] (-0.261,0.114] ... (0.56,2.07]
table(fact)
#> fact
#>  (-1.86,-0.918] (-0.918,-0.261]  (-0.261,0.114]    (0.114,0.56]     (0.56,2.07] 
#>               4               4               4               4               4