3
f=function(s,abc=0,def=0) {
  return(s+def)
}

f(3)
[1] 3
f(3,d=4)
[1] 7

why R match "d" with "def"?

Is there a rule that says when the name of a variable cannot be found in a function then R will automatically match a name whose capital letters are just the same with it?

elixiao
  • 182
  • 1
  • 1
  • 8
  • 1
    R uses [partial matching](http://stackoverflow.com/questions/14153904/partial-matching-of-function-argument). – Arun Apr 02 '13 at 14:42

2 Answers2

6

This is described in the Argument Matching section of the R language definition.

This subsection applies to closures but not to primitive functions. The latter typically ignore tags and do positional matching, but their help pages should be consulted for exceptions, which include log, round, signif, rep and seq.int.

The first thing that occurs in a function evaluation is the matching of formal to the actual or supplied arguments. This is done by a three-pass process:

Exact matching on tags. For each named supplied argument the list of formal arguments is searched for an item whose name matches exactly. It is an error to have the same formal argument match several actuals or vice versa.

Partial matching on tags. Each remaining named supplied argument is compared to the remaining formal arguments using partial matching. If the name of the supplied argument matches exactly with the first part of a formal argument then the two arguments are considered to be matched. It is an error to have multiple partial matches. Notice that if f <- function(fumble, fooey) fbody, then f(f = 1, fo = 2) is illegal, even though the 2nd actual argument only matches fooey. f(f = 1, fooey = 2) is legal though since the second argument matches exactly and is removed from consideration for partial matching. If the formal arguments contain ‘...’ then partial matching is only applied to arguments that precede it.

Positional matching. Any unmatched formal arguments are bound to unnamed supplied arguments, in order. If there is a ‘...’ argument, it will take up the remaining arguments, tagged or not.

Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
6

There are three main methods by which arguments are matched. These are discussed in the Argument Matching section of the R Language Definition, the relevant section of which I reproduce below:

The first thing that occurs in a function evaluation is the matching of formal to the actual or supplied arguments. This is done by a three-pass process:

  1. Exact matching on tags. For each named supplied argument the list of formal arguments is searched for an item whose name matches exactly. It is an error to have the same formal argument match several actuals or vice versa.
  2. Partial matching on tags. Each remaining named supplied argument is compared to the remaining formal arguments using partial matching. If the name of the supplied argument matches exactly with the first part of a formal argument then the two arguments are considered to be matched. It is an error to have multiple partial matches. Notice that if f <- function(fumble, fooey) fbody, then f(f = 1, fo = 2) is illegal, even though the 2nd actual argument only matches fooey. f(f = 1, fooey = 2) is legal though since the second argument matches exactly and is removed from consideration for partial matching. If the formal arguments contain ... then partial matching is only applied to arguments that precede it.
  3. Positional matching. Any unmatched formal arguments are bound to unnamed supplied arguments, in order. If there is a ... argument, it will take up the remaining arguments, tagged or not.

Note that this is what happens with normal functions. Primitve functions are special in R. Primitives are special in many ways and invariably don't use tags but use positional matching instead. This is not consistently applied however.

Another important exception is arguments in functions that include a ... argument. As mentioned above, if an argument occurs after ... in the argument list of a function, you must name that argument in a call to that function if as positional matching is not applied to these arguments. For example:

foo <- function(x, ..., y) {
    y
}

The only way to pass a value to y is to name it in the function call, e.g.:

foo(y = 1)
Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453