NULL 預設值
本練習屬於課程
R 函式撰寫入門
練習說明
- 更新
cut_by_quantile()的定義,讓labels引數的預設值為NULL。 - 從對
cut_by_quantile()的呼叫中移除labels引數。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Set the default for labels to NULL
cut_by_quantile <- function(x, n = 5, na.rm = FALSE, labels, 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 labels argument from the call
cut_by_quantile(
n_visits,
labels = c("very low", "low", "medium", "high", "very high"),
interval_type = "(lo, hi]"
)