[R] Problem with sample session

Greg Snow 538280 at gmail.com
Fri May 30 18:20:45 CEST 2014


If you pay attention and are careful not to use any variables names
that conflict then you do not need a work around (and the conflicts
function can help you see if there are any conflicts that you may need
to worry about).

Probably the best work around is to use the with or within function
instead of attaching.  For a couple of quick commands these work great
and I prefer them to using attach.  But, sometimes for a long sequence
of commands attach is much more convenient and is fine to use as long
as you recognize the potential dangers and are careful.

On Thu, May 29, 2014 at 3:56 PM, Stephen Meskin <actuary at umbc.edu> wrote:
> Thanks Greg for your response. Is there a work around?
>
> Of course this begs the question as to Why is attach part of the sample session in App. A of the introductory manual? All the commands are directly from App. A. Is it possible the configuration of R on my computer is not in accord with acceptable practice? I.e. Could my configuration be set so that attach works as App. A intends?
> If not then App. A needs to be changed to replace attach.
> If so, then App. A needs to provide instructing on appropriate configuration of R for newbies.
>
> Stephen Meskin
> Sent from my iPad
>
>> On May 29, 2014, at 1:06 PM, Greg Snow <538280 at gmail.com> wrote:
>>
>> 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
>>



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



More information about the R-help mailing list