Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active Jan 7, 2021
Embed
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){
if(!is.numeric(col)){
warning("Values passed to Equibucket are not numeric")
return(NA)
}
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)
res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment