Other sites

R … three dots ellipsis

If you have used R before, then you surely have come across the three dots, e.g.

print(x, ...)

In technical language, this is called an ellipsis. And it means that the function is designed to take any number of named or unnamed arguments.

By the way, the ellipsis is not a specialty of R. Other programming languages have the same or a similar concept. For example, C# has the params key-word, which is a type-safe variation of the three dots.

In the example of the print function, any argument passed after the initial x will simply be passed along to a subclass’ print function. For example, if you look at the generic

print.data.frame

, it has more arguments than the

print.default

function. And it passes the

...

when it calls print function on the matrix:

print.data.frame

The interesting question is: How do you write functions that make use of ellipsis? The answer is very simple: you simply convert the … to a list, like so:

HelloWorld

So, when should I use the ellipsis construct?

That, again, is very simple: there are essentially two situations when you can use the three dots:

When it makes sense to call the function with a variable number of arguments. See the

HellowWorld

example above. Another very prominent example is the

paste

function.

When, within your function, you call other functions, and these functions can have a variable number of arguments, either because

the called function is generic (like in the above

print

example), or

the called function can be passed into the function as an argument, as for example with the

FUN

argument in apply:

apply

Advanced Stuff

When you want to manipulate the … , and pass part of it to another recursive call, you can use the