#' Another version of cut()
#'
#' Cut values into ranges
#' @param value vector of values to be cut
#' @param number of categories (equal ranges)
#' @param format If "normal" then labels are specified as in cut (eg. '(0, 1]'). If 'percent' then label is in percent format denoting percentile refering to range of provided values.
#' @param round specifies if new labels has to be rounded (as in base::round())
#' @return $intervval_value
#' @return $interval_label
#' @examples
#' value <- runif(100,0,100)
#' cut2( value , length = 4, round=2, quantile=T )
#' cut2( value , length = 4, round=2, quantile=T , return='percent')
#' @export
cut2 <- function(value, length=4, return = "labs", round=2, quantile=T){
min <- min(value, na.rm=T)
max <- max(value, na.rm=T)
if(quantile){
# equal number in bin
breaks <- unique(quantile(value, probs = seq(0, 1, length.out = length+1)))
} else {
# equal range of bin
breaks <- seq(min, max, by = (max-min)/(length))
}
if(return=='percent') {
labels = round( (breaks - min) / (max-min) , round )
value %>%
cut(breaks = breaks, labels = scales:::percent(labels[-1]) , include.lowest=T, dig.lab=10) %>%
return()
} else {
value %>%
cut(breaks = round(breaks, round), include.lowest=T, dig.lab=10) %>%
return()
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.