Skip to content

Instantly share code, notes, and snippets.

Last active Jan 7, 2021
What would you like to do?
Slice a skewed distribution into buckets with equal populations
#' num_bkts should be a non-zero integer
#' col should contain numeric values
equi_bucket <- function(col, num_bkts){
warning("Values passed to Equibucket are not numeric")
xmin = min(col, na.rm = TRUE)
eps = 1/(num_bkts-1)
zo_intervals <- seq(0,1, eps)
buckets <- unname(quantile(col, zo_intervals,
na.rm = TRUE))
rng <- range(col, na.rm=TRUE)[2] -
range(col, na.rm=TRUE)[1]
return( (buckets - xmin)/rng)
res <- equi_bucket(test, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment