Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of Dot / Period in R Functions

Tags:

r

I recently saw a function in R where someone had used . as an argument. I can't seem to find any documentation on this (other than the use of ellipsis or "dot-dot-dot"). Can someone either point me in the direction of documentation or provide an example of usage?

hello.world <- function(.) "Hello World"
# function(.) is what I'm asking about.
like image 604
Brandon Bertelsen Avatar asked Mar 11 '12 06:03

Brandon Bertelsen


People also ask

What does the dot do 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 dot variable mean in R?

The dot you see with the is_spam~. command means that there are no explanatory variables. Typically with model formulas, you will see y~x, but if you have no x variable, y~.

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 is a period in R?

Periods track the change in the "clock time" between two date-times. They are measured in common time related units: years, months, days, hours, minutes, and seconds. Each unit except for seconds must be expressed in integer values.


2 Answers

Dot is a a valid character in symbol names just like any letter, so . is no different than let's say a - it has no special meaning in this context. You can write things like:

> . <- 10
> . + .
[1] 20

It may look strange but is valid in R. The above use function(.) is let's say unusual, but syntactically valid. Since the author did not reference . in the function body, we will never know if he meant ... or just used it because he could.

like image 87
Simon Urbanek Avatar answered Oct 16 '22 16:10

Simon Urbanek


While the answer given by Simon Urbanek is correct here's a reason the . is preferred over other characters as a function argument. In some situations, like lapply a function needs to receive an argument by design. But if there is no need of the argument inside the function we still need a dummy argument name. A . is the smallest character - almost invisible, so you are not distracted by what the function is delivering. The function(.) is as good as function() but has an advantage that it will not give an error when used in apply family.

like image 3
Lazarus Thurston Avatar answered Oct 16 '22 17:10

Lazarus Thurston