Skip to content

Instantly share code, notes, and snippets.

@Ram-N
Last active January 7, 2021 21:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ram-N/ad6f9608db134971d7692762fb4a6a6a to your computer and use it in GitHub Desktop.
Save Ram-N/ad6f9608db134971d7692762fb4a6a6a to your computer and use it in GitHub Desktop.
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