R … three dots ellipsis

February 2, 2015

(This article was first published on ipub » R, and kindly contributed to R-bloggers)

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


 , it has more arguments than the


  function. And it passes the


  when it calls print function on the matrix:

print.data.frame <- function (x, ..., digits = NULL, quote = FALSE, right = TRUE,         
    row.names = TRUE)                                                 
    n <- length(row.names(x))                                         
    if (length(x) == 0L) {                                            
        #some other code...
    else {                                                            
        m <- as.matrix(format.data.frame(x, digits = digits,          
            na.encode = FALSE))                                       
        if (!isTRUE(row.names))                                       
            dimnames(m)[[1L]] <- if (identical(row.names, FALSE))     
                rep.int("", n)                                        
            else row.names                                            
        print(m, ..., quote = quote, right = right)                   

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 <- function(...) {
  arguments <- list(...)

HelloWorld("Hello", "World", "!")

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:

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

      example above. Another very prominent example is the



  2. When, within your function, you call other functions, and these functions can have a variable number of arguments, either because
    1. the called function is generic (like in the above

       example), or

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

       argument in apply:

apply <- function (X, MARGIN, FUN, ...)                                        
  #...lots of code

      for (i in 1L:d2) {                                            
            tmp <- FUN(newX[, i], ...)                                
            if (!is.null(tmp))                                        
                ans[[i]] <- tmp                                       
  # ...more code

 Advanced Stuff

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



The post R … three dots ellipsis appeared first on ipub.

To leave a comment for the author, please follow the link and comment on their blog: ipub » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)