Two dots, ".." in R

..n refers to the nth element in ....

Here's a slightly simpler alternative to Simon's answer, avoiding eval/parse.

f <- function(...)
{
  message("dots = ")
  print(list(...))   # Notice that you need to convert ...
                     # to a list or otherwise evaluate it
  message("..1 = ")
  print(..1)
  message("..2 = ")
  print(..2)
}

f(runif(5), letters[1:10])
## dots = 
## [[1]]
## [1] 0.94707123 0.09626337 0.41480592 0.83922757 0.94635464
## 
## [[2]]
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"
## 
## ..1 = 
## [1] 0.94707123 0.09626337 0.41480592 0.83922757 0.94635464
## ..2 = 
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j"

The accepted answers make sense.

But I saw two dots in a completely different context (i.e. not inside a function), like this: ..prop..:

ggplot(data=diamonds) + 
  geom_bar(
   mapping=aes(x=cut, y=..prop.., group=1) 
  )

Turns out ..prop.. are special variables created by ggplot's stat_count transformation.

stat_count provides two internal variables ..count.. and ..prop.., referring to count and proportion respectively. Don’t be surprised by the ..name.. notation, it is used to prevent confusion with your own columns (don’t name your own columns with weird names like ..count..!)

(Remember variable names in R can include periods. I come from Python background, so this double-period seems like Python's double-underscore convention: __prop__, a technique used to mark special / "private" variables / "name mangle" those variables)


..4 would be a reserved word in R's parser. Under ?Reserved you will find

... and ..1, ..2 etc, which are used to refer to arguments passed down from a calling function.

Example

#  Function will return nth element from ... ( n MUST be a named argument)
f <- function( ... , n = NULL )
   return( eval( parse( text = paste0( ".." , n ) ) ) )

#  Return third element of ...
f( n = 3 , 1:3 , 3:1 , 10:15 )
#[1] 10 11 12 13 14 15

#  Try to return element that is out of bounds
f( n = 4 , 1:3 , 3:1 , 10:15 )
#Error in eval(expr, envir, enclos) : 
#  the ... list does not contain 4 elements

Now that you know what it is, how do you use it? Courtesy of John Chambers;

"The name ..1 refers to the first matching argument, ..2 to the second, etc. You should probably avoid this obscure convention, which can usually be done by writing a function with some ordinary argument names, and calling it with "...""

Software for Data Analysis: Programming with R, John M. Chambers, Springer-Verlag, New York, 2008.
Excerpt from page 457.

Tags:

R