Categorical defaults
When cutting up a numeric vector, you need to worry about what happens if a value lands exactly on a boundary. You can either put this value into a category of the lower interval or the higher interval. That is, you can choose your intervals to include values at the top boundary but not the bottom (in mathematical terminology, "open on the left, closed on the right", or (lo, hi]
). Or you can choose the opposite ("closed on the left, open on the right", or [lo, hi)
). cut_by_quantile()
should allow these two choices.
The pattern for categorical defaults is:
function(cat_arg = c("choice1", "choice2")) {
cat_arg <- match.arg(cat_arg)
}
Free hint: In the console, type head(rank)
to see the start of rank()
's definition, and look at the ties.method
argument.
This exercise is part of the course
Introduction to Writing Functions in R
Exercise instructions
- Update the signature of
cut_by_quantile()
so that theinterval_type
argument can be"(lo, hi]"
or"[lo, hi)"
. Note the space after each comma. - Update the body of
cut_by_quantile()
to match theinterval_type
argument. - Remove the
interval_type
argument from the call tocut_by_quantile()
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Set the categories for interval_type to "(lo, hi]" and "[lo, hi)"
cut_by_quantile <- function(x, n = 5, na.rm = FALSE, labels = NULL,
interval_type) {
# Match the interval_type argument
___ <- ___
probs <- seq(0, 1, length.out = n + 1)
qtiles <- quantile(x, probs, na.rm = na.rm, names = FALSE)
right <- switch(interval_type, "(lo, hi]" = TRUE, "[lo, hi)" = FALSE)
cut(x, qtiles, labels = labels, right = right, include.lowest = TRUE)
}
# Remove the interval_type argument from the call
cut_by_quantile(n_visits, interval_type = "(lo, hi]")