Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do ..1 and ..2 stand for in R? [duplicate]

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?

like image 299
qed Avatar asked Oct 25 '14 13:10

qed


1 Answers

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
like image 192
Thomas Avatar answered Sep 22 '22 06:09

Thomas