[Experimental]

This function transforms a numeric vector into a factor corresponding to the quantile intervals. The intervals are left-open and right-closed.

cut_quantile(x, percentiles = c(1/3, 2/3), digits = 0)

Arguments

x

(numeric)
the continuous variable values which should be cut into quantile bins. NA values are not taken into account when computing quantiles and are attributed to the NA interval.

percentiles

(proportions)
the required percentiles for the quantile intervals to be generated. Duplicated values are removed.

digits

(integer)
the precision to use when formatting the percentages.

Value

The factor with a description of the available quantiles as levels.

Examples

set.seed(452)
x <- runif(10, -10, 10)
cut_quantile(x, c(0.33333333, 0.6666666), digits = 4)
#>  [1] [0%,33.3333%]       (33.3333%,66.6667%] [0%,33.3333%]      
#>  [4] (66.6667%,100%]     (33.3333%,66.6667%] [0%,33.3333%]      
#>  [7] (66.6667%,100%]     (33.3333%,66.6667%] (33.3333%,66.6667%]
#> [10] (66.6667%,100%]    
#> Levels: [0%,33.3333%] (33.3333%,66.6667%] (66.6667%,100%]

x[1:4] <- NA
cut_quantile(x)
#>  [1] <NA>       <NA>       <NA>       <NA>       (33%,67%]  [0%,33%]  
#>  [7] (67%,100%] (33%,67%]  [0%,33%]   (67%,100%]
#> Levels: [0%,33%] (33%,67%] (67%,100%]