In the body of some R functions, for example lm
I see calls to the match.call
function. As its help page says, when used inside a function match.call
returns a call where argument names are specified; and this is supposed to be useful for passing a large number of arguments to another functions.
For example, in the lm
function we see a call to the function model.frame
...
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
{
cl <- match.call()
mf <- match.call(expand.dots = FALSE)
m <- match(c("formula", "data", "subset", "weights", "na.action",
"offset"), names(mf), 0L)
mf <- mf[c(1L, m)]
mf$drop.unused.levels <- TRUE
mf[[1L]] <- quote(stats::model.frame)
mf <- eval(mf, parent.frame())
...
...Why is this more useful than making a straight call to model.frame
specifying the argument names as I do next?
function (formula, data, subset, weights, na.action, method = "qr",
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
contrasts = NULL, offset, ...)
{
mf <- model.frame(formula = formula, data = data,
subset = subset, weights = weights, subset = subset)
...
(Note that match.call
has another use that I do not discuss, store the call in the resulting object.)
See Question&Answers more detail:
os