rdata.tabledplyrnomenclature

What do . (dot) and % (percentage) mean in R?


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:

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.


Solution

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

    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