[R] Problem with sample session

Greg Snow 538280 at gmail.com
Thu May 29 19:06:33 CEST 2014


This is a warning and in your case would not be a problem, but it is
good to think about and the reason why it is suggested that you avoid
using attach and be very careful when you do use attach.  What is
happening is that you first created a vector named 'x' in your global
workspace, you then create a data frame that contains a column that is
a copy of 'x' that is also named 'x' and the data frame also has
another column named 'y'.  You then later attach the data frame to the
search list (if you run the 'search()' command you will see your
search list).  This is convenient in that you can now access 'y' by
typing its name instead of something like 'dummy$y', but what happens
if you just type x?  The issue is that there are 2 objects on your
search path with that same name.  For your example it will not matter
much because they have the same value, but what if you run a command
like 'x <- 3', now you will see a single value instead of a vector of
length 20 which can lead to hard to find errors.  This is why R tries
to be helpful by warning you that there are multiple objects named 'x'
and therefore you may not be accessing the one that you think.  If you
use attach without being careful it is possible to plot (or regress or
...) one variable from one dataset against another variable from a
completely unrelated dataset and end up with meaningless results.  So,
if you use attach, be careful.  You may also want to look at the
followng functions for help with dealing with these issues: conflicts,
find, get, with, within

On Wed, May 28, 2014 at 11:55 PM, Stephen Meskin <actuary at umbc.edu> wrote:
> While following the suggestion in the manual "An Introduction to R" to
> begin with Appendix A, I ran into the problem shown below about 3/4 of
> the way down the 1st page of App. A.
>
> After using the function /attach/, I did not get visible columns in the
> data frame as indicated but the rather puzzling message emphasized below.
>
> I am running R version 3.1.0 (2014-04-10) using Windows XP.  Thanks in
> advance for your help.
>
>> > x<-1:20
>> > w<-1+sqrt(x)/2
>> > dummy<-data.frame(x=x, y=x+rnorm(x)*w)
>> > dummy
>>     x         y
>> 1   1  2.885347
>> ...
>> > fm<- lm(y ~ x, data=dummy)
>> > summary(fm)
>>
>> Call:
>> ...
>> > fm1<- lm(y ~ x, data=dummy,weight=1/w^2)
>> > summary(fm1)
>>
>> Call:
>> ...
>> > attach(dummy)
>> *_The following object is masked _by_ .GlobalEnv:
>>
>>     x_**_
>> _*
>
> --
> /Stephen A Meskin/, PhD, FSA, MAAA
> Adjunct Assistant Professor of Mathematics, UMBC
>
> **//
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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.



-- 
Gregory (Greg) L. Snow Ph.D.
538280 at gmail.com



More information about the R-help mailing list