Using with rather than attach

Advanced R users do not routinely employ attach in their work, because it can lead to unexpected problems in resolving names (e.g. you can end up with multiple copies of the same variable name, each of a different length and each meaning something completely different). Most modelling functions like lm or glm have a data = argument so attach is unnecessary in those cases. Even when there is no data = argument it is preferable to wrap the call using with like this

with(data, function( ))

The with function evaluates an R expression in an environment constructed from data. You will often use the with function other functions like tapply or plot which have no built-in data argument. If your dataframe is part of the built-in package called datasets (like OrchardSprays) you can refer to the dataframe directly by name:

with(OrchardSprays,boxplot(decrease~treatment))

Here we calculate the number of ‘no’ (not infected) cases in the bacteria dataframe which is part of the MASS library:

library(MASS)
with(bacteria,tapply((y=="n"),trt,sum))

placebo  drug  drug+
     12    18     13

and here we plot brain weight against body weight for mammals on log-log axes:

with(mammals,plot(body,brain,log="xy"))

without attaching either dataframe. Here is an unattached dataframe called reg.data:

reg.data<-read.table("c:\\temp\\regression.txt",header=T)

with which we carry out a linear regression and print a summary

with (reg.data, {
                 model<-lm(growth~tannin)
                 summary(model) } ) ...

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.