match.arg(arg, choices, several.ok = FALSE) matches arg against a table of candidate values as specified by choices, where NULL means to take the first one.
In the example below:
- type store the argument passed in parameter ("mean", "median", "trimmed")
- the argument passed should be one of “mean”, “median”, “trimmed”
center <- function(x, type = c("mean", "median", "trimmed")) {
type <- match.arg(type)
switch(type,
mean = mean(x),
median = median(x),
trimmed = mean(x, trim = .1))
}