[R] Confused with Student's sleep data description

peter dalgaard pdalgd at gmail.com
Fri Jan 27 19:41:49 CET 2012


On Jan 27, 2012, at 17:18 , R. Michael Weylandt wrote:

> It doesn't have anything to do with attach (which is naughty in other ways!)  rather it's the internal representation of categorical variables (R speak: factors) that store each level as an integer for memory efficiency but print things with string levels so they look nice to the user. 
> 
> You'll note there's a 1-to-1 match between Ctl-1 an Trt-2 in your data. 
> 
> The funny business (best I reckon) is that use of $ which down-grades your data to its internal representation as a numeric (integer) vector. 

Rubbish! There must be more to this:

> data(sleep)
> sleep$group
 [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
Levels: 1 2
> attach(sleep)
> group
 [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
Levels: 1 2
 
Presumably there's a "group" variable with different factor levels sitting in the global environment. "$" certainly will not "down-grade" data to integers (much less keep them as factors but modify the level set)

-pd 

> 
> But yes, you should avoid attach anyways. 
> 
> M
> 
> On Jan 27, 2012, at 6:03 AM, Олег Девіняк <o.devinyak at gmail.com> wrote:
> 
>> I am confused whether Student's sleep data "show the effect of two
>> soporific drugs" or Control against Treatment (one drug). The reason
>> is the next:
>>> require(stats)
>>> data(sleep)
>>> attach(sleep)
>>> extra[group==1]
>> numeric(0)
>>> group
>> [1] Ctl Ctl Ctl Ctl Ctl Ctl Ctl Ctl Ctl Ctl Trt Trt Trt Trt Trt Trt Trt Trt Trt
>> [20] Trt
>> Levels: Ctl Trt
>>> sleep$group
>> [1] 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2
>> Levels: 1 2
>> 
>> Does some package overwrite my attach()? I am worried mostly in the
>> right performance of my code by others. So have the attach() to be
>> avoided?
>> Thanks for answers!
>> 
>> ______________________________________________
>> 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.
> 
> ______________________________________________
> 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