Where did lost variables go

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Where did lost variables go

David Parkhurst
I have several variables in a data frame that aren't listed by ls()
after I attach that data frame.  Where did they go, and how can I stop
the hidden ones from masking the local ones?
Thanks for any help.
David

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Where did lost variables go

szehnder@uni-bonn.de
A reproducible example would do well here David

Best

Simon
On 31 Dec 2013, at 02:42, David Parkhurst <[hidden email]> wrote:

> I have several variables in a data frame that aren't listed by ls()
> after I attach that data frame.  Where did they go, and how can I stop
> the hidden ones from masking the local ones?
> Thanks for any help.
> David
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Where did lost variables go

Berwin A Turlach-3
In reply to this post by David Parkhurst
G'day David,

On Mon, 30 Dec 2013 20:42:53 -0500
David Parkhurst <[hidden email]> wrote:

Some wild guesses in the absence of a reproducible example.

> I have several variables in a data frame that aren't listed by ls()
> after I attach that data frame.

ls() list the objects in the global environment.  If you attach a data
frame it is attached to the search path, typically after the global
environment.  

Type 'search()' to see your search path.

ls() list the global environment, the first entry in the list and
called ".GlobalEnv".  Your data frame should be listed as an object in
that environment.  

Assuming the name of your data frame is 'foo', then there should be the
name 'foo' somewhere in the list of names returned by 'search()'.
Assuming 'foo' is listed in the second position, then 'ls(2)' should
list all the objects found at that location of the search path, i.e.
all the variables in your data frame.

> Where did they go,

See above.

> and how can I stop the hidden ones from masking the local ones?

Do you mean with "local ones" those in the global environment and by
"hidden ones" those that you couldn't find?  I.e. is there an object
"bar" listed by 'ls()' but also an object "bar" listed by 'ls(2)' (i.e.
your data frame 'foo' contained a variable with name 'bar')?  Then it is
the other way round, the local ones are hiding the hidden ones.  

For that reason attaching data frames has its dangers.  It allows to
easily access the variables in the data frame, but any changes to a
variable creates a local copy.  Thus, any change *will* not propagate
back to the data frame!  

Hopefully the commands below will clarify further.

Cheers,

        Berwin


R> foo <- data.frame(bar=rnorm(2), fubar=runif(2))
R> ls()
[1] "foo"
R> attach(foo)
R> search()
 [1] ".GlobalEnv"        "foo"               "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"    
R> ls(2)
[1] "bar"   "fubar"
R> bar
[1] -0.07741633  1.05804653
R> fubar
[1] 0.08516929 0.82718383
R> bar <- "what now"
R> ls()
[1] "bar" "foo"
R> bar
[1] "what now"
R> ls(2)
[1] "bar"   "fubar"
R> get("bar", pos=2)
[1] -0.07741633  1.05804653
R> foo
          bar      fubar
1 -0.07741633 0.08516929
2  1.05804653 0.82718383
R> detach(2)
R> bar
[1] "what now"
R> fubar
Error: object 'fubar' not found
R> foo
          bar      fubar
1 -0.07741633 0.08516929
2  1.05804653 0.82718383
R> attach(foo)
The following object is masked _by_ .GlobalEnv:

    bar
R> bar
[1] "what now"
R> fubar
[1] 0.08516929 0.82718383
R> detach(2)
R> bar
[1] "what now"
R> fubar
Error: object 'fubar' not found
R> foo
          bar      fubar
1 -0.07741633 0.08516929
2  1.05804653 0.82718383

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Where did lost variables go

Bert Gunter
Gents:

I would add that:

1) attach() should probably no longer be used in R, for all the
reasons (and more) cited,

2)  The preferred alternative these days is to use lists, including
data frames, as containers and make liberal use of the  ?with and
?within  functions. Environments can also be useful, but are more
complicated as their semantics differ. S4 classes and objects are
probably also relevant.

Comments, criticisms, links, additions, and subtractions welcome, as
this issue comes up regularly here and it would be nice to have
consensus wisdom to refer to.

Cheers,


Bert Gunter
Genentech Nonclinical Biostatistics
(650) 467-7374

"Data is not information. Information is not knowledge. And knowledge
is certainly not wisdom."
H. Gilbert Welch




On Tue, Dec 31, 2013 at 2:14 AM, Berwin A Turlach
<[hidden email]> wrote:

> G'day David,
>
> On Mon, 30 Dec 2013 20:42:53 -0500
> David Parkhurst <[hidden email]> wrote:
>
> Some wild guesses in the absence of a reproducible example.
>
>> I have several variables in a data frame that aren't listed by ls()
>> after I attach that data frame.
>
> ls() list the objects in the global environment.  If you attach a data
> frame it is attached to the search path, typically after the global
> environment.
>
> Type 'search()' to see your search path.
>
> ls() list the global environment, the first entry in the list and
> called ".GlobalEnv".  Your data frame should be listed as an object in
> that environment.
>
> Assuming the name of your data frame is 'foo', then there should be the
> name 'foo' somewhere in the list of names returned by 'search()'.
> Assuming 'foo' is listed in the second position, then 'ls(2)' should
> list all the objects found at that location of the search path, i.e.
> all the variables in your data frame.
>
>> Where did they go,
>
> See above.
>
>> and how can I stop the hidden ones from masking the local ones?
>
> Do you mean with "local ones" those in the global environment and by
> "hidden ones" those that you couldn't find?  I.e. is there an object
> "bar" listed by 'ls()' but also an object "bar" listed by 'ls(2)' (i.e.
> your data frame 'foo' contained a variable with name 'bar')?  Then it is
> the other way round, the local ones are hiding the hidden ones.
>
> For that reason attaching data frames has its dangers.  It allows to
> easily access the variables in the data frame, but any changes to a
> variable creates a local copy.  Thus, any change *will* not propagate
> back to the data frame!
>
> Hopefully the commands below will clarify further.
>
> Cheers,
>
>         Berwin
>
>
> R> foo <- data.frame(bar=rnorm(2), fubar=runif(2))
> R> ls()
> [1] "foo"
> R> attach(foo)
> R> search()
>  [1] ".GlobalEnv"        "foo"               "package:stats"
>  [4] "package:graphics"  "package:grDevices" "package:utils"
>  [7] "package:datasets"  "package:methods"   "Autoloads"
> [10] "package:base"
> R> ls(2)
> [1] "bar"   "fubar"
> R> bar
> [1] -0.07741633  1.05804653
> R> fubar
> [1] 0.08516929 0.82718383
> R> bar <- "what now"
> R> ls()
> [1] "bar" "foo"
> R> bar
> [1] "what now"
> R> ls(2)
> [1] "bar"   "fubar"
> R> get("bar", pos=2)
> [1] -0.07741633  1.05804653
> R> foo
>           bar      fubar
> 1 -0.07741633 0.08516929
> 2  1.05804653 0.82718383
> R> detach(2)
> R> bar
> [1] "what now"
> R> fubar
> Error: object 'fubar' not found
> R> foo
>           bar      fubar
> 1 -0.07741633 0.08516929
> 2  1.05804653 0.82718383
> R> attach(foo)
> The following object is masked _by_ .GlobalEnv:
>
>     bar
> R> bar
> [1] "what now"
> R> fubar
> [1] 0.08516929 0.82718383
> R> detach(2)
> R> bar
> [1] "what now"
> R> fubar
> Error: object 'fubar' not found
> R> foo
>           bar      fubar
> 1 -0.07741633 0.08516929
> 2  1.05804653 0.82718383
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Where did lost variables go

JohnDee
In reply to this post by David Parkhurst
On Mon, 30 Dec 2013 20:42:53 -0500
David Parkhurst <[hidden email]> wrote:

> I have several variables in a data frame that aren't listed by ls()
> after I attach that data frame.  Where did they go, and how can I
> stop the hidden ones from masking the local ones?
> Thanks for any help.
> David
>
You really need to offer more information, e.g. a reproducible example
that includes just how how moved data into your data frame. As
it is, the only reasonable suggestion is to try and find your lost
singleton socks, the missing variablee may be hiding with them.
Alternatively the lost variables may have taken off to Never Never Land
to hang with the Lost Boys.

One simple possibility is that the missing variables were actually
never read in to the data frame.  Have you ever seen those variables
in your data frame?  Did you try str(<dataframe>)?

As regards "hidden" and "local," you'll - again - have to be a good
deal more explicit.  

JDougherty

______________________________________________
[hidden email] 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.