[R] no doubt a dumb question, but..

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Nov 9 14:46:59 CET 2004


On Tue, 2004-11-09 at 02:07, Martin Maechler wrote:

<SNIP>

> 
> Further note that many of us try to avoid attach()ing data frames
> (most of the time; not always) and we have provided the nice alternative
>     with( <data> ,  <expression_body> ) 
> 
> So, for the current example, you could also say
> 
>   with(trees, plot(Girth ~ Height))
> 
> The main advantage of with(): Only inside it, the components of
> 'trees' are visible - no need to remember to  detach() ;
> see also  help(with) and its examples.

Right. Thanks Martin. After sending my reply, I kicked myself a few
times for not mentioning with()...especially because it also enables a
standardized mechanism for accessing data frame variables across
functions (not all of which have a 'data = ' argument).

Also, there is a substantial savings with respect to overhead by not
attaching and detaching (manipulating the search path), which I had
noted and benchmarked previously in a post, earlier this year I believe,
that I cannot locate at the moment. 

This can become very important if one is looping (in some fashion) over
data frames or subsets of data frames and therefore avoids the repeated
calls to attach() and detach().

Best regards,

Marc




More information about the R-help mailing list