[R] Where did lost variables go, with example

peter dalgaard pdalgd at gmail.com
Thu Jan 2 14:01:40 CET 2014

On 31 Dec 2013, at 17:32 , Duncan Murdoch <murdoch.duncan at gmail.com> wrote:

> On 13-12-31 9:48 AM, David Parkhurst wrote:
>> Two or three respondents asked for an example of my problem.  Here's
>> what's happening to me now.  I can't reproduce how I got to this point,
>> though:
>>  > ls()
>> [1] "All8"   "All8Sites"  "A"   "B"  "C"  "i"  "n"  "D"  "F"
>>  > X
>> Error: object 'X' not found
>>  > attach(All8Sites)
>>  > ls()
>> [1] "All8"  "All8Sites"  "A"  "B"  "C"  "i"  "n"  "D"  "F"
>> "X" is one of the variables in the data frame I attached in the third
>> command above, but it's not listed by >ls().  If I enter > X now, its
>> values ARE listed, but it's hiding somewhere.  What is happening here?
>> How can I get the variables in that data frame listed when I attach it?
> Use search() to see the search list.  Your dataframe will likely be in position 2.  Use ls(2) to see the variables there.

Or ls("All8Sites"). Notice, by the way, that this is subtly but importantly different from ls(All8Sites). E.g.,

> attach(airquality)
> ls(2)
[1] "Day"     "Month"   "Ozone"   "Solar.R" "Temp"    "Wind"   
> ls(airquality)
[1] "Day"     "Month"   "Ozone"   "Solar.R" "Temp"    "Wind"   
> search()
 [1] ".GlobalEnv"        "airquality"        "package:stats"    
 [4] "package:graphics"  "package:grDevices" "package:utils"    
 [7] "package:datasets"  "package:methods"   "Autoloads"        
[10] "package:base"     
> ls("airquality")
[1] "Day"     "Month"   "Ozone"   "Solar.R" "Temp"    "Wind"   
> rm(Month, pos=2)
> ls(airquality)
[1] "Day"     "Month"   "Ozone"   "Solar.R" "Temp"    "Wind"   
> ls("airquality")
[1] "Day"     "Ozone"   "Solar.R" "Temp"    "Wind"   
> detach()
> ls("airquality")
Error in as.environment(pos) : 
  no item called "airquality" on the search list


> Duncan Murdoch
> ______________________________________________
> 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.

Peter Dalgaard, Professor
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com

More information about the R-help mailing list