Skip to content

newbie questions: attached data.frames and rm()

2 messages · Alojz Loui Polic, Peter Dalgaard

#
Hi

I'm just starting with R-plus and I have noticed
the following behaviour.

If a data.frame is attached and then removed before 
being detached, the data is still present in the 
path and can accessed. Is this expected behaviour, 
and if so why? 

Thank you 

Loui

below is sample code to show this, I'm using R-1.2.2
on Win-NT4


AA <- c(1, 1, 1, 1, 1)
BB <- c(2, 3, 2, 1, 2)
data1 <- data.frame( AA.data=AA, BB.data=BB )
ls()
attach(data1)
ls(2)

rm(data1)
ls()
ls(2)
AA.data[1]
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Alojz Loui Polic <lpolic at ineural.com> writes:
?????
It's designed that way. Whether or not that is a good thing is
debatable - people easily get bitten from attaching a data frame and
modifying it without that affecting the attached version. One upside
is that the argument to attach() can be an arbitrary expression, e.g.

AA <- c(1, 1, 1, 1, 1)
BB <- c(2, 3, 2, 1, 2)
attach(data.frame( AA.data=AA, BB.data=BB)

[It is not a trivial exercise to think up an alternative semantic
model for attached objects. One problem being that modifying an object
in R is more often than not a compute-and-rename operation, so
accessing an object by reference is not possible (although reference
by name is).]