Take the 2-minute tour ×
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It's 100% free, no registration required.

I'm just reading the book "R in a Nutshell". And it seems as if I skipped the part where the "." as in "sample.formula" was explained.

> sample.formula <- as.formula(y~x1+x2)

Is sample an object with a field formula as in other languages? And if so, how can I find out, what other fields/functions this object has? (Type declaration)

EDIT: I just found another confusing use of the ".":

> svm(formula = is_spam~., data = spambase.training)

(the dot between ~., )

share|improve this question
    
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. This is the same as the model $y=\beta_0$ –  Christopher Aden May 12 '11 at 20:06
11  
@Christopher On the contrary, the . in the formula tells R to use all variables in the dataframe spambase.training (except is_spam) as predictors. The model $y = \beta_{0}$ is fit with y ~ 1. –  caracal May 12 '11 at 20:25
    
I didn't check sources beforehand. Thank you for the correction! –  Christopher Aden May 12 '11 at 23:31
    
@caracal (+1) Wow I was just wondering how to do this. Thanks! –  Thomas Levine May 13 '11 at 4:37

3 Answers 3

up vote 28 down vote accepted

The dot can be used as in normal name. It has however additional special interpretation. Suppose we have an object with specific class:

 a <- list(b=1)
 class(a) <- "myclass"

Now declare myfunction as standard generic in the following way:

 myfunction <- function(x,...) UseMethod("myfunction")

Now declare the function

 myfunction.myclass <- function(x,...) x$b+1

Then the dot has special meaning. For all objects with class myclass calling

 myfunction(a)

will actualy call function myfunction.myclass:

 > myfunction(a)
  [1] 2

This is used widely in R, the most apropriate example is function summary. Each class has its own summary function, so when you fit some model for example (which usually returns object with specific class), you need to invoke summary and it will call appropriate summary function for that specific model.

share|improve this answer
    
I am very surprised this answer was accepted and upvoted so much, because it doesn't answer the question at all! It refers to the ellipsis ... (which is a single lexeme, not a sequence of three different ones) as a "dot" whereas the question clearly means a dot . as used in formulas and names in an entirely different way, as correctly described in a contemporaneous answer by Chase. –  whuber Jun 12 at 20:39
1  
Well I do not refer to the ellipsis. I tried to explain that the dot is used for S3 method dispatch. The generic functions usually have ellipses, that is why I used them. If they are removed from the code, the answer would not change. I can only guess that I gave the answer before the edit, since I would give a different answer now after rereading the question body. –  mpiktas Jun 12 at 21:14
    
Thank you for the explanation. I think the appearance of "..." two times misled me into believing you were referring to it as a "dot". –  whuber Jun 12 at 23:13

Look at the help page for ?formula with regard to . Here's the relevant bits:

There are two special interpretations of . in a formula. The usual one is in the context of a data argument of model fitting functions and means ‘all columns not otherwise in the formula’: see terms.formula. In the context of update.formula, only, it means ‘what was previously in this part of the formula’.

Alternatively, the reshape and reshape2 packages use . and ... a bit differently (from ?cast):

There are a couple of special variables: "..." represents all other variables not used in the formula and "." represents no variable

share|improve this answer

There are some exceptions (S3 method dispatch), but generally it is simply used as legibility aid, and as such has no special meaning.

share|improve this answer
2  
I would say the opposite - it has special meaning (the S3 dispatch you mention), but some old naming conventions caused function names that are not S3 generics to have names that include a .. That pertains to names of functions. As for names of (non-function) objects, then yes, there is no special meaning. –  Gavin Simpson May 12 '11 at 15:54
    
At the beginning of a name, a . makes the object invisible to ls() though. –  caracal May 12 '11 at 20:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.