Using attach in This Book

I use attach throughout this book because experience has shown that it makes the code easier to understand for beginners. In particular, using attach provides simplicity and brevity, so that we can

  • refer to variables by name, so x rather than dataframe$x
  • write shorter models, so lm(y~x) rather than lm(y~x,data=dataframe)
  • go straight to the intended action, so plot(y~x) not with(dataframe,plot(y~x))

Nevertheless, readers are encouraged to use with or data= for their own work, and to avoid using attach wherever possible.

Get The R Book now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.