63

What is the meaning of the ~. argument in R?

For example plot(~.,xyz..)

I have seen this argument used several times in various contexts and since it is difficult to meaningfully search for symbols on google, I have met little success.

eli-k
  • 10,898
  • 11
  • 40
  • 44
Info5ek
  • 1,227
  • 4
  • 17
  • 25

1 Answers1

57

This is a formula, in a shorthand notation. Try this:

plot( mpg ~ cyl, data= mtcars )

The left hand is the dependent variable, the right hand is the independent variable. Much like y = bx + c means that y ~ x.

Formulas are one of the corner stones of R, and you will need to understand them to use R efficiently. Most frequently, formulas are used in modeling of all sorts, for example you can do basic linear regression with

lm( mpg ~ wt, data= mtcars )

...to see how mileage per gallon depend on weight. Take a look at ?formula for some more explanations.

The dot means "any columns from data that are otherwise not used". Google for "R formulas" to get more information.

January
  • 16,320
  • 6
  • 52
  • 74
  • 10
    Technically it means, to paraphrase from Alice in Wonderland, whatever the function you call with it wants it to mean. Functions can play with formulae and data in many many ways, so the single interpretation may not always be correct. – Spacedman Nov 19 '12 at 08:20
  • 1
    According to http://seananderson.ca/2013/10/19/reshape.html, the right hand is the "dependent" variable and the one(s) on the left are the IDs or the independent variables. – Abdalrahman Shatou Mar 17 '16 at 14:52
  • 6
    but you haven't explained the meaning of the dot. – skan Dec 05 '16 at 18:47
  • 9
    The dot here means remaining columns. Try `plot( cyl ~ ., data= mtcars )`, console will prompt **Hit to see next plot:**, and you will see the plot of cyl vs. mpg, cyl vs. disp, ... etc. – allenyllee Feb 17 '18 at 06:48
  • 4
    If you do not specify dependent variable, says `plot( ~ ., data= mtcars )`, It will give you a matrix of plot, from mpg vs. mpg to carb vs. carb – allenyllee Feb 17 '18 at 06:57