R/percentile_cut.R

Defines functions percentify_cut

Documented in percentify_cut

#' Group a data.frame by percentile ranges in variable by cutting points
#'
#' This function is a small wrapper around \code{\link{percentify}}. This
#' function takes a vector of points and creates ranges between those points,
#' resulting in non-overlapping groups.
#'
#' There is a [ggplot2::autoplot()] to visualize the the percentile ranges.
#'
#' @inheritParams percentify
#' @param q Numerical values for cutting points. Must be between 0
#'   and 1.
#'
#' @return percentile grouped tibble
#' @export
#'
#' @family percentile samplers
#'
#' @seealso [cut_evenly]
#'
#' @examples
#' library(dplyr)
#' library(broom)
#' percent_mtcars <- percentify_cut(mtcars, mpg, c(0.25, 0.5, 0.75))
#'
#' percent_mtcars
#'
#' summarize(percent_mtcars,
#'           mean_hp = mean(hp),
#'           mean_wt = mean(wt),
#'           n_obs = n()
#'           )
#'
#' percent_mtcars %>%
#'   group_modify(~tidy(lm(disp ~ wt + cyl, data = .x)))
#'
#' # cut_evenly() can be used to create cuts
#' percentify_cut(mtcars, mpg, cut_evenly(8))
#'
#' library(ggplot2)
#' autoplot(percent_mtcars)
percentify_cut <- function(data, var, q = numeric()) {
  percentify(data, {{var}}, lower = c(0, q), upper = c(q, 1))
}
EmilHvitfeldt/percentify documentation built on July 9, 2019, 10:54 p.m.