papirrin
papirrin

Reputation: 2053

Two dots, ".." in R

This may be a lame question, but anyways. In RStudio, I just noticed that typing a number, .. , and another number will change the syntax highlight of the characters from navy blue to baby blue for the .. and the numbers after it.

As an example, this is a number that has this distinctive color:

4..4

The part "..4" has the baby blue color.

I am using the default syntax coloring. I tried in the interpreter to introduce such constant but I only got the error "Error: unexpected numeric constant in "4..5", and the queries with "two dots" or .. does not seem to be very google friendly.

Does anyone knows what is the usage of ".." is, if any?

Upvotes: 28

Views: 10933

Answers (3)

Nate Anderson
Nate Anderson

Reputation: 21146

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)

Upvotes: 9

Simon O'Hanlon
Simon O'Hanlon

Reputation: 59990

..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.

Upvotes: 31

Richie Cotton
Richie Cotton

Reputation: 121127

..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"

Upvotes: 16

Related Questions