[R-sig-teaching] pros/cons of teaching attach()

Joshua Wiley jwiley.psych at gmail.com
Thu Sep 23 17:09:07 CEST 2010


On Thu, Sep 23, 2010 at 5:53 AM, Laura Chihara <lchihara at carleton.edu> wrote:
> This thread is interesting. For those who use the
> subset command, when you extract a variable from a
> data frame, do you give it the same name as in the data set?

For me this would depend on whether I would be merging the data back
in and/or whether I would be using the data set (and original
variable) again.  For example,

conc <- subset(DNase, select = "conc")
# now forget about DNase
# various operations with conc
--but--
conc.tmp <- subset(DNase, select = "conc") # I often add .tmp or 2 or something
# do some stuff (data cleaning, transformations, whatever)
DNase[ , "conc"] <- conc.tmp
# on to model fitting or whatever with full data

That said unless there is reason to be doing alot of work with only
one variable from the data set or to assign changes you may want to
reverse, my preference would always be to leave it in the data set (it
was in there for a reason, after all).

>
> For instance,
>
> my.data with variables trout, whale
>
> trout <- subset(my.data, select=trout, drop=T)

If I was only selecting entire variables, I would just use one of the
extraction operators, e.g., my.data[, "trout"]

>
> I guess if you never attach, then you don't have to
> worry about masking in the future.
>
> Thanks
>
> -Laura
> On 9/21/2010 11:34 PM, Joshua Wiley wrote:
>>
>> Hi all,
>>
>> I am wondering if anyone teaches the attach function to new students
>> and if so, why?
>>
>>
>> I have never taught a class using R, but in small tutorials and
>> providing direct help, I have run across this issue several times.  I
>> have always avoided it, but a couple of times, students had learned to
>> use it in a class (though without much detail) and were rather grumpy
>> with me for using full variable names or with().  New converts,
>> particularly from GUIs like SPSS, already tend to be leery of R, and I
>> hate to make working at the command line more onerous since any typing
>> is more than they are used to, but at the same time a discussion of
>> environments, which I think is necessary to avoid trouble with attach,
>> does not seem like beginner material either.
>>
>> I have wondered about this many times, but was just reminded again by
>> an R-help post.
>>
>> Thanks,
>>
>> Josh
>>
>
> --
> ************************************************
> Laura Chihara
> Professor of Mathematics  507-222-4065 (office)
> Dept of Mathematics       507-222-4312 (fax)
> Carleton College
> 1 North College Street
> Northfield MN 55057
>
> _______________________________________________
> R-sig-teaching at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-teaching
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/




More information about the R-sig-teaching mailing list