Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two dots, ".." in R

Tags:

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?

like image 391
papirrin Avatar asked Nov 15 '13 09:11

papirrin


People also ask

What does 2 dots mean in R?

two dots is the second time derivative. so a dot is the same as d/dt. 5.

What do dots mean in R?

A dot in function name can mean any of the following: nothing at all. a separator between method and class in S3 methods. to hide the function name.

What does two dots mean in JavaScript?

First dot is actually a decimal point, just let JavaScript Compiler know the second dot wants to invoke property or method.


3 Answers

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

like image 188
Simon O'Hanlon Avatar answered Oct 23 '22 17:10

Simon O'Hanlon


..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"
like image 24
Richie Cotton Avatar answered Oct 23 '22 17:10

Richie Cotton


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)

like image 30
The Red Pea Avatar answered Oct 23 '22 15:10

The Red Pea