R/cut_quantile.R
cut_quantile.Rd
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, ...)
An R object, usually a numeric vector.
A single integer with the number of breaks to use.
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()
.
A factor()
is returned, unless labels = FALSE
(in this case, a
integer vector is obtained).
# 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.26,0.136] (-0.918,-0.26] (-0.918,-0.26] (0.56,2.07] (0.56,2.07]
#> [6] (-1.86,-0.918] (0.136,0.56] (-1.86,-0.918] (-0.918,-0.26] (-0.26,0.136]
#> [11] (0.136,0.56] (-0.918,-0.26] (0.136,0.56] (0.136,0.56] (-1.86,-0.918]
#> [16] (0.56,2.07] (0.56,2.07] (-0.26,0.136] (-1.86,-0.918] (-0.26,0.136]
#> 5 Levels: (-1.86,-0.918] (-0.918,-0.26] (-0.26,0.136] ... (0.56,2.07]
table(fact)
#> fact
#> (-1.86,-0.918] (-0.918,-0.26] (-0.26,0.136] (0.136,0.56] (0.56,2.07]
#> 4 4 4 4 4