Skip to content
Prev 22740 / 398502 Next

Peculiar behavior of attached objects

Prof. Ripley, 

Thanks for your quick reply.  It was nice to hear an answer from one of
the experts in the field.  I agree that this behavior of R is as
documented, and found a good summary on p.29 of "An Introduction to R"
at

http://cran.r-project.org/doc/manuals/R-intro.pdf

When I had read the shorter online documentation, I had assumed that the
attach() command was like the "import" command of Python or the "use"
statement of Fortran90, which are very useful for avoiding name clashes
or allowing the usage of short names to refer to variables with longer
names in a package or module.  It still seems to me that requiring an R
user to detach() and attach() after each change of a dataframe variable
in order to be able to access it by a short name is somewhat awkward. 
Perhaps this is such an intrinsic part of the language that it would be
hard to extend, in which case the developers might consider adding a new
command which had more of the flexibility of the Python "import" command
or Fortran90 "use" statement.  (Of course it's easy for me to suggest
such a change, and much harder for someone to actually implement it...)

For example, in python, one can refer to variables by their fully
qualified module names:
3.1415926535897931

or by a shorter name defined inside a module:
3.1415926535897931

or if there is a name clash with another variable by the same name it
can be renamed:
3.1415926535897931

Similar things can be done with the Fortran90 "use" statement, which I
know does not create a new copy of a variable with separate memory
storage, which would be inefficient and awkward to use, it is just a new
name that points to the same location in memory.

More info at:

http://www.python.org/doc/current/tut/tut.html
http://www.python.org/doc/current/ref/import.html
http://w3.pppl.gov/~hammett/comp/f90tut/f90.tut7.html


As for my "explanation", I had assumed that it was only the action of
assigning to d$y while attach(d) was in effect that created a new
variable called y that was a copy of the original value of the dataframe
variable.  I guess you are telling me that a copy of the whole dataframe
is made every time an attach() command is issued.  I had assumed that
only new pointers with short names were created by an attach() command
(which is what the documentation seemed to imply to me), as it seems
inefficient to make a copy of a whole dataframe at every attach()
command, particularly for a very large database.

Thanks,

Greg

----------------------------------------------------------------------------
ripley at stats.ox.ac.uk wrote:
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._