[R] deciphering help for `attach`

PIKAL Petr petr@p|k@| @end|ng |rom prechez@@cz
Thu Apr 30 15:33:40 CEST 2020


Hallo

Sorry for confusion. John explained in technical language what I was trying to explain in plain one. One cannot simply change values in original data frame or call them, however they could be used in functions.

Consider this

attach(cars)
speed <- speed*2
speed
 [1]  8  8 14 14 16 18 20 20 20 22 22 24 24 24 24 26 26 26 26 28 28 28 28 30 30
[26] 30 32 32 34 34 34 36 36 36 36 38 38 38 40 40 40 40 40 44 46 48 48 48 48 50
plot(speed, dist)
fit <- lm(dist~speed, data=cars)
abline(fit)
fit1 <- lm(dist~speed)
abline(fit1, col = 2)

And if you attach something which has speed and dist variable inside, the things could be even more confusing.

mydata <- data.frame(speed="Blue", dist="Black")
attach(mydata)
The following object is masked _by_ .GlobalEnv:

    speed

The following objects are masked from cars:

    dist, speed

> search()
 [1] ".GlobalEnv"        "mydata"            "cars"             
 [4] "package:stats"     "package:datasets"  "package:fun"      
 [7] "package:utils"     "package:grDevices" "package:graphics" 
[10] "package:methods"   "Autoloads"         "package:base"     

So although mydata are attached its speed variable is accessible only by referencing to mydata, but now dist from mydata is used in lm, if cars data frame is not specified.

> fit <- lm(dist~speed)
Error in model.frame.default(formula = dist ~ speed, drop.unused.levels = TRUE) : 
  variable lengths differ (found for 'speed')

> speed
 [1]  8  8 14 14 16 18 20 20 20 22 22 24 24 24 24 26 26 26 26 28 28 28 28 30 30
[26] 30 32 32 34 34 34 36 36 36 36 38 38 38 40 40 40 40 40 44 46 48 48 48 48 50
> dist
[1] "Black"

I used attach myself a lot about 20 years ago but due to unexpected (by myself) behaviour I do not use it now. Of course nobody will prevent you to use or teach attach, but you has to acknowledge that attach is doing what it is doing and not what you or your students expect it is doing. And for me, environments are still something I did not familiarise with.

Cheers
Petr

> -----Original Message-----
> From: Edward McNeil <edward.m using psu.ac.th>
> Sent: Monday, April 27, 2020 3:26 PM
> To: PIKAL Petr <petr.pikal using precheza.cz>
> Cc: r-help using r-project.org
> Subject: RE: [R] deciphering help for `attach`
> 
> Dear Petr,
> Thanks for your quick reply. Much appreciated. However, you haven't really
> answered either of my questions, although I don't quite understand your
> reference to La Gioconda.
> 
> In any case, despite your strong recommendation not to use `attach`, I am
> going to keep using it, as I have done successfully for the past 16 years, and
> keep teaching it, until it either kills me or disappears from R. Unfortunately I
> have to teach R to students and I don't like it when they ask me "tricky"
> questions to which I have no answer. ;)
> --
> Edward McNeil
> 
> On Mon, April 27, 2020 8:00 pm, PIKAL Petr wrote:
> Hi.
> 
> I strongly recommend not to use attach. I agree that mentioned statements
> are rather contradictory and probably others could give you more insightful
> answer. You could consider that by attaching some data, you create
> something like a copy of original data in your system with a feature that you
> can use column names directly. If you change something in the data after
> attachment, you change only attached version and not an original.
> 
> It is similar as if you take a picture of Gioconda an use some creativity to add
> a moustache to this picture. In any circumstances moustache does not
> propagate to the original Louvre painting. Do not perform any tricks,
> preferably do not perform attach.
> 
> Cheers
> Petr
> 
> > -----Original Message-----
> > From: R-help <r-help-bounces using r-project.org> On Behalf Of Edward McNeil
> > Sent: Monday, April 27, 2020 2:07 PM
> > To: r-help using r-project.org
> > Subject: [R] deciphering help for `attach`
> >
> > Hi,
> > I have two related questions.
> >
> > 1. In the help page for `attach` under "Details" it says in paragraph 3:
> > "By default the database is attached ..."
> >
> > But then paragraph 4 starts: "The database is not actually attached."
> >
> > Could somebody explain this contradiction? Is the data(base) attached
> > or not?
> >
> > 2. What is meant by the 5th paragraph: "One useful ‘trick’ is to use
> > what = NULL (or equivalently a length-zero list) to create a new
> > environment on the search path into which objects can be assigned by
> `assign` ... "?
> >
> > I don't understand what this "trick" is or why a "trick" needs to be
> > performed here.
> >
> > Thanks
> > --
> > Edward McNeil
> >
> > ______________________________________________
> > R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-
> > guide.html and provide commented, minimal, self-contained,
> > reproducible code.
> 
> 
> 
> 



More information about the R-help mailing list