[R] Masking

Ista Zahn istazahn at gmail.com
Sun Sep 15 00:40:21 CEST 2013


Hi David,

On Sat, Sep 14, 2013 at 5:49 PM, David Arnold <dwarnold45 at suddenlink.net> wrote:
> Hi,
>
> I'm a bit confused by masking. Thought I understood, but today makes me
> wonder.
>
> x=0:9
> y=0:9
> sm=data.frame(x=4:8,y=9:13)
> attach(sm)
> x
> detach(sm)
>
> The code produces this message:
>
>> attach(sm)
> The following object is masked _by_ .GlobalEnv:
>
>     x, y
>
>> x
>  [1] 0 1 2 3 4 5 6 7 8 9
>
> I guess I thought that when you attach a dataframe, then the values in the
> dataframe now override any existing variables in your workspace with the
> same name. But this example seems to say that if I have something in my data
> frame with the same name as an existing variable in my workspace, if I
> attach the data frame, then it won't be the variable in the data frame that
> is in play, but the existing variable in my workspace.

Apparently. I never use attach, so never paid much attention.

>
> Have I learned my lesson?

Maybe, but see below.

>
> And what are those commands that help you see what will be searched first,
> second, third ... for a variable in your code?

The lesson you still need to learn is to read the documentation when
you are confused or uncertain about something. The answers to all your
questions are in ?attach which clearly tells you that the second
argument is

"     pos: integer specifying position in ‘search()’ where to attach."

(i.e., seach() is the function that tells you what will be searched
and in what order) and that

"By default the database is attached in position 2 in the search
     path, immediately after the user's workspace and before all
     previously attached packages and previously attached databases.
     This can be altered to attach later in the search path with the
     ‘pos’ option, but you cannot attach at ‘pos = 1’"

Best,
Ista

>
> D.
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Masking-tp4676137.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.



More information about the R-help mailing list