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] (-1.45,-0.378]   (-0.0338,0.559]  (-2.44,-1.45]    (-0.0338,0.559] 
#>  [5] (0.559,2.07]     (0.559,2.07]     (-2.44,-1.45]    (-0.378,-0.0338]
#>  [9] (-0.378,-0.0338] (-0.378,-0.0338] (-1.45,-0.378]   (0.559,2.07]    
#> [13] (0.559,2.07]     (-2.44,-1.45]    (-0.0338,0.559]  (-2.44,-1.45]   
#> [17] (-1.45,-0.378]   (-0.378,-0.0338] (-0.0338,0.559]  (-1.45,-0.378]  
#> 5 Levels: (-2.44,-1.45] (-1.45,-0.378] (-0.378,-0.0338] ... (0.559,2.07]
table(fact)
#> fact
#>    (-2.44,-1.45]   (-1.45,-0.378] (-0.378,-0.0338]  (-0.0338,0.559] 
#>                4                4                4                4 
#>     (0.559,2.07] 
#>                4