[R] Confused with Student's sleep data description

R. Michael Weylandt <michael.weylandt@gmail.com> michael.weylandt at gmail.com
Fri Jan 27 17:18:04 CET 2012


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. 

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.



More information about the R-help mailing list