I am (probably) NOT referring to the "all other variables" meaning like var1~.
here. I was pointed to plyr
once again and looked into mlply
and 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.
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.
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).
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.
A dot is not allowed.
A dot in function name can mean any of the following:
The dot in data.frame
doesn't separate data
from frame
, other than visually.
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(...))
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
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With