LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Introduction to Writing Functions in R

Kurs anzeigen

Anleitung zur Übung

  • Update the signature of cut_by_quantile() so that the interval_type argument can be "(lo, hi]" or "[lo, hi)". Note the space after each comma.
  • Update the body of cut_by_quantile() to match the interval_type argument.
  • Remove the interval_type argument from the call to cut_by_quantile().

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# 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]")
Code bearbeiten und ausführen