Last active
January 7, 2021 21:50
-
-
Save Ram-N/ad6f9608db134971d7692762fb4a6a6a to your computer and use it in GitHub Desktop.
Slice a skewed distribution into buckets with equal populations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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