Search code examples
rreserved-words

What do ..1 and ..2 stand for in R?


Quote from R language definition:

Notice that identifiers starting with a period are not by default listed by the ls function and that ‘...’ and ‘..1’, ‘..2’, etc. are special.

The following identifiers have a special meaning and cannot be used for object names if else repeat while function for in next break TRUE FALSE NULL Inf NaN NA NA_integer_ NA_real_ NA_complex_ NA_character_ ... ..1 ..2 etc.

However it does not give any further detail. Could anyone elaborate?


Solution

  • These are used to positionally extract values from the ... argument of a function. See example below:

    myfun <- function(...) {
       list(a = ..1, b = ..2, c = ..3)
    }
    
    myfun(1,2,3)
    # $a
    # [1] 1
    # $b
    # [1] 2
    # $c
    # [1] 3
    
    myfun(3,2,1)
    # $a
    # [1] 3
    # $b
    # [1] 2
    # $c
    # [1] 1
    
    myfun(1:5, "hello", letters[1:3])
    # $a
    # [1] 1 2 3 4 5
    # $b
    # [1] "hello"
    # $c
    # [1] "a" "b" "c"
    

    This use becomes obvious if you try to call one of these from the console:

    > ..1
    Error: ..1 used in an incorrect context, no ... to look in