16

My question might sound stupid but I have noticed that . and % is often used in R and to be frank I don't really know why it is used.

I have seen it in dplyr (go here for an example) and data.table (i.e. .SD) but I am sure it must be used in other place as well.

Therefore, my question is:

  • What does . mean? Is it some kind of R coding best practice nomenclature? (i.e. _functionName is often used in javascript to indicate it is a private function). If yes, what's the rule?
  • Same question for %, which is also often used in R (i.e. %in%,%>%,...).

My guess always has been that . and % are a convenient way to quickly call function but the way data.table uses . does not follow this logic, which confuses me.

Community
  • 1
  • 1
zipp
  • 1,046
  • 13
  • 26
  • % is just used in a lot of operators, you can define your own operator as well, for example `"%nin%" – DeanAttali Dec 19 '14 at 09:45
  • See also [**here**](http://stackoverflow.com/questions/22314680/how-to-use-the-operator-in-r). Just note that `%.%` discussed in that question is deprecated, and `%>%` should be used now. – Henrik Dec 19 '14 at 11:44
  • @Henrik and @daattali, thanks for your answer. I knew about it. My question was more about if there is any programming rule as when to code with ```%``` or ```.``` that MrFlick answered to. – zipp Dec 19 '14 at 15:25
  • 1
    For % see: http://stackoverflow.com/questions/27125672/what-does-mean-in-r/27129032#27129032 – G. Grothendieck Dec 19 '14 at 15:42
  • See also [What does the dot mean in R – personal preference, naming convention or more?](http://stackoverflow.com/questions/7526467/what-does-the-dot-mean-in-r-personal-preference-naming-convention-or-more) – Henrik Feb 28 '16 at 10:16

2 Answers2

26

. has no inherent/magical meaning in R. It's just another character that you can use in symbol names. But because it is so convenient to type, it has been given special meaning by certain functions and conventions in R. Here are just a few

  • . is used look up S3 generic method implementations. For example, if you call a generic function like plot with an object of class lm as the first parameter, then it will look for a function named plot.lm and, if found, call that.
  • often . in formulas means "all other variables", for example lm(y~., data=dd) will regress y on all the other variables in the data.frame dd.
  • libraries like dplyr use it as a special variable name to indicate the current data.frame for methods like do(). They could just as easily have chosen to use the variable name X instead
  • functions like bquote use .() as a special function to escape variables in expressions
  • variables that start with a period are considered "hidden" and will not show up with ls() unless you call ls(all.names=TRUE) (similar to the UNIX file system behavior)

However, you can also just define a variable named my.awesome.variable<-42 and it will work just like any other variable.

A % by itself doesn't mean anything special, but R allows you to define your own infix operators in the form %<something>% using two percent signs. If you define

`%myfun%` <- function(a,b) {
    a*3-b*2
}

you can call it like

5 %myfun% 2
# [1] 11
NelsonGon
  • 11,358
  • 5
  • 21
  • 44
MrFlick
  • 163,738
  • 12
  • 226
  • 242
  • Ok thank you. I was wondering if it had any special convention naming and you perfectly answered it. Thanks. – zipp Dec 19 '14 at 06:07
  • 8
    A couple of other uses for `.`: In model formulas, e.g., `lm(var ~ ., data=df)` the period means "all other variables in `df` besides `var`." In `dplyr` it's used as a "pronoun" to refer to the current data frame (see [here](http://blog.rstudio.org/2014/05/21/dplyr-0-2/), for example). – eipi10 Dec 19 '14 at 06:09
  • @eipi10 thanks. That's why I got confused it is often in lot of cases. I was wondering if there was some kind of logic behind it. – zipp Dec 19 '14 at 06:12
  • 7
    Also, `.` before a variable makes it "invisible" : if you do `.a – Cath Dec 19 '14 at 08:10
13

MrFlick's answer doesn't cover the usage of . in data.table;

In data.table, . is (essentially) an alias for list, so any* call to [.data.table that accepts a list can also be passed an object wrapped in .().

So the following are equivalent:

DT[ , .(x, y)]
DT[ , list(x, y)]

*well, not quite. any use in the j argument, yes; elsewhere is a work in progress, see here.

MichaelChirico
  • 31,197
  • 13
  • 98
  • 169
  • Thank you for your contribution. A side note is that the question was not where it is used but if it had any special meaning such as `_` being used for private. – zipp Feb 14 '17 at 15:04
  • @zipp the "special meaning" is `list`. And `_` being used for "private" is nothing official. – MichaelChirico Feb 14 '17 at 15:34