Skip to Content
R in a Nutshell
book

R in a Nutshell

by Joseph Adler
January 2010
Beginner
634 pages
19h 50m
English
O'Reilly Media, Inc.
Content preview from R in a Nutshell

Argument Order and Named Arguments

When you specify a function in R, you assign a name to each argument in the function. Inside the body of the function, you can access the arguments by name. For example, consider the following function definition:

> addTheLog <- function(first, second) {first + log(second)}

This function takes two arguments, called first and second. Inside the body of the function, you can refer to the arguments by these names.

When you call a function in R, you can specify the arguments in three different ways (in order of priority):

  1. Exact names. The arguments will be assigned to full names explicitly given in the argument list. Full argument names are matched first:

    > addTheLog(second=exp(4),first=1)
    [1] 5
  2. Partially matching names. The arguments will be assigned to partial names explicitly given in the arguments list:

    > addTheLog(s=exp(4),f=1)
    [1] 5
  3. Argument order. The arguments will be assigned to names in the order in which they were given:

    > addTheLog(1,exp(4))
    [1] 5

When you are using generic functions, you cannot specify the argument name of the object on which the generic function is being called. You can still specify names for other arguments.

When possible, it’s a good practice to use exact argument names. Specifying full argument names does require extra typing, but it makes your code easier to read and removes ambiguity.

Partial names are a deprecated feature because they can lead to confusion. As an example, consider the following function:

> f <- function(arg1=10,arg2=20) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

R in a Nutshell, 2nd Edition

R in a Nutshell, 2nd Edition

Joseph Adler
The Big R-Book

The Big R-Book

Philippe J. S. De Brouwer
R Packages

R Packages

Hadley Wickham

Publisher Resources

ISBN: 9781449377502Supplemental ContentErrata Page