[R] Problem with data.frames

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 4 13:06:27 CEST 2003


On Mon, 4 Aug 2003, Simon Fear wrote:

> Peter is right, but there is a point here: it would be nice if
> attach(my.dframe) would do nothing - or at least warn - if my.dframe was
> already in the search list. Like library(). Attaching twice is almost

It is not like library().  attach attaches a copy of the data frame, and
it can be altered subsequently. So how can you tell if the data frame is
on the search list.  I suspect it is quite common to create an object
called `tmp' and put it on the search list for a while.  library() assumes
that you have not reinstalled a loaded package (and if you have you would
get inconsistent results).

> bound
> to be an error?

Not at all.

> Of course in many circumstances it might be better to use with() than
> attach() - if you haven't come across with(), it works like attach()
> with an
> inbuilt detach() when the parentheses close.
> 
> Aside 1: it would also be nice if ?attach pointed to ?with. Is this kind
> of
> suggestion best sent to r-help or r-devel?

I don't see they do the same job, especially not in an interactive 
session (which is when attach() is most usefully used in this way).

> Aside 2: is with() efficient, or does it create a copy of the dataset? 

It does not create a copy, as you can see from the code (it uses a 
three-argument eval).

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list