In the help file for some functions, the defaults are sometimes given as vectors. An example is:
?base::rank
This opens a help file in which the usage is shown.
Usage:
rank(x, na.last = TRUE,
ties.method = c("average", "first", "last", "random", "max", "min"))
In this example the default for na.last
is TRUE
. But, the default for ties.method
is given as a vector. What exactly does this mean in terms of which is chosen by default? And, more importantly, why is it written this way in the first place?
Thanks
We can also specify a random default value for the vector. In order to do so, below is the approach: Syntax: // For declaring vector v(size, default_value); // For Vector with a specific default value // here 5 is the size of the vector // and 10 is the default value vector v1(5, 10);
Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.
A vector function is a function that takes one or more variables and returns a vector. We'll spend most of this section looking at vector functions of a single variable as most of the places where vector functions show up here will be vector functions of single variables.
A default argument is a value provided in a function declaration that is automatically assigned by the compiler if the calling function doesn't provide a value for the argument. In case any value is passed, the default value is overridden.
The default is the first value. It is written that way so that you can see at a glance all of the possible options.
Internally, the function will use match.arg
to evaluate user input and match it to the vector used. This matching is done using pmatch
(p for partial matching), so that the argument can be abbreviated. For example, rank(x, "first")
can be abbreviated to rank(x, "f")
. See ?match.arg
for more details. Quoting the ?match.arg
Description:
match.arg
matchesarg
against a table of candidate values as specified bychoices
, whereNULL
means to take the first one.
match.arg
is commonly used when there are a small to medium number of possible options for an argument.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With