Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function Default Given as Vector

Tags:

function

r

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

like image 787
bk18 Avatar asked May 01 '18 13:05

bk18


People also ask

How do you assign a default value to a vector?

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);

How do you define a function with default value?

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.

Can you have a vector of functions?

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.

What is the default function in function?

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.


1 Answers

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 matches arg against a table of candidate values as specified by choices, where NULL 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.

like image 86
Gregor Thomas Avatar answered Sep 29 '22 09:09

Gregor Thomas