Aliases: match.arg
Keywords: programming
### ** Examples require(stats) ## Extends the example for 'switch' 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)) } x <- rcauchy(10) center(x, "t") # Works
[1] -0.2019009
center(x, "med") # Works
[1] -0.1539195
try(center(x, "m")) # Error
Error in match.arg(type) : 'arg' should be one of "mean", "median", "trimmed"
stopifnot(identical(center(x), center(x, "mean")), identical(center(x, NULL), center(x, "mean")) ) ## Allowing more than one 'arg' and hence more than one match: match.arg(c("gauss", "rect", "ep"), c("gaussian", "epanechnikov", "rectangular", "triangular"), several.ok = TRUE)
[1] "gaussian" "rectangular" "epanechnikov"
match.arg(c("a", ""), c("", NA, "bb", "abc"), several.ok=TRUE) # |--> "abc"
[1] "abc"