ask Categorical defaults

# 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 = c("(lo, hi]", "[lo, hi)")) {
  # Match the interval_type argument
  interval_type <- match.arg(interval_type)
  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

This is a tutorial from Datacamp. I don't understand what the meaning of Categorical defaults is. Can anyone help me?

Following tutorials and asking questions you get from that is fine with respect to Homework Policy, as far as I understand (not totally sure though). But based on all your previous topics, I think you should at least try to figure out the answers by now.

In this particular example, the function definition indicates that as an interval_type, only two specified values are accepted. If you don't specify one while calling cut_by_quantile, the first one of those will be used.

This is ensured in match.arg. If you pass some other value, you should get an error, something like this:

Error in match.arg(interval_type) : 
  'arg' should be one of “(lo, hi]”, “[lo, hi)” 
stop(gettextf("'arg' should be one of %s", paste(dQuote(choices), 
    collapse = ", ")), domain = NA) 
<what you called>

Note: Obviously it won't be "<what you called>", but it'll be the exact function call that you will make.

Afterwards, it'll use switch to choose the type of intervals you want (basically if-else, which is more readable in my opinion). Then that'll be used in cut finally.

Hope this helps.