Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the dot mean in R – personal preference, naming convention or more?

I am (probably) NOT referring to the "all other variables" meaning like var1~. here. I was pointed to plyr once again and looked into mlplyand wondered why parameters are defined with leading dot like this:

function (.data, .fun = NULL, ..., .expand = TRUE, .progress = "none",  .parallel = FALSE)  { if (is.matrix(.data) & !is.list(.data))      .data <- .matrix_to_df(.data) f <- splat(.fun) alply(.data = .data, .margins = 1, .fun = f, ..., .expand = .expand,      .progress = .progress, .parallel = .parallel) } <environment: namespace:plyr> 

What's the use of that? Is it just personal preference, naming convention or more? Often R is so functional that I miss a trick that's long been done before.

like image 940
Matt Bannert Avatar asked Sep 23 '11 08:09

Matt Bannert


People also ask

What does the dot 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~. says to guess at the value of y using no other variables.

What does dot mean in function?

The biggest idea behind using the dot is to treat a function of two variables as a family of functions of one variable, indexed by the other. For example, say I have a function f(x,y). I want to talk about, for a particular fixed y, the one variable function in x given by f(x,y).

What is naming convention in R?

Naming conventions in R are famously anarchic, with no clear winner and multiple conventions in use simultaneously in the same package. This has been written about before, in a lucid article in the R Journal, a detailed exploration of names in R source code hosted on CRAN and general discussion on stackoverflow.

Can variable name have dot?

A dot is not allowed.


1 Answers

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

Possible meanings

1. Nothing at all

The dot in data.frame doesn't separate data from frame, other than visually.

2. Separation of methods and classes in S3 methods

plot is one example of a generic S3 method. Thus plot.lm and plot.glm are the underlying function definitions that are used when calling plot(lm(...)) or plot(glm(...))

3. To hide internal functions

When writing packages, it is sometimes useful to use leading dots in function names because these functions are somewhat hidden from general view. Functions that are meant to be purely internal to a package sometimes use this.

In this context, "somewhat hidden" simply means that the variable (or function) won't normally show up when you list object with ls(). To force ls to show these variables, use ls(all.names=TRUE). By using a dot as first letter of a variable, you change the scope of the variable itself. For example:

x <- 3 .x <- 4  ls() [1] "x"  ls(all.names=TRUE) [1] ".x" "x"   x [1] 3 .x [1] 4 

4. Other possible reasons

In Hadley's plyr package, he uses the convention to use leading dots in function names. This as a mechanism to try and ensure that when resolving variable names, the values resolve to the user variables rather than internal function variables.


Complications

This mishmash of different uses can lead to very confusing situations, because these different uses can all get mixed up in the same function name.

For example, to convert a data.frame to a list you use as.list(..)

as.list(iris) 

In this case as.list is a S3 generic method, and you are passing a data.frame to it. Thus the S3 function is called as.list.data.frame:

> as.list.data.frame function (x, ...)  {     x <- unclass(x)     attr(x, "row.names") <- NULL     x } <environment: namespace:base> 

And for something truly spectacular, load the data.table package and look at the function as.data.table.data.frame:

> library(data.table)  > methods(as.data.table) [1] as.data.table.data.frame* as.data.table.data.table* as.data.table.matrix*         Non-visible functions are asterisked   > data.table:::as.data.table.data.frame function (x, keep.rownames = FALSE)  {     if (keep.rownames)          return(data.table(rn = rownames(x), x, keep.rownames = FALSE))     attr(x, "row.names") = .set_row_names(nrow(x))     class(x) = c("data.table", "data.frame")     x } <environment: namespace:data.table> 
like image 145
Andrie Avatar answered Oct 05 '22 14:10

Andrie