Alex Rivera | Logout

Do you use attach() or call variables by name or slicing?

Asked 2009-08-21T05:45:34.667
26

Many intro R books and guides start off with the practice of attaching a data.frame so that you can call the variables by name. I have always found it favorable to call variables with $ notation or square bracket slicing [,2]. That way I can use multiple data.frames without confusing them and/or use iteration to successively call columns of interest. I noticed Google recently posted coding guidelines for R which included the line

1) attach: avoid using it

How do people feel about this practice?

Edit
Report

1 Answer

3

Just like Leoni said, with and within are perfect substitutes for attach, but I wouldn't completely dismiss it. I use it sometimes, when I'm working directly at the R prompt and want to test some commands before writing them on a script. Especially when testing multiple commands, attach can be a more interesting, convenient and even harmless alternative to with and within, since after you run attach, the command prompt is clear for you to write inputs and see outputs.

Just make sure to detach your data after you're done!

answered 2012-01-26T14:38:38.733

Your Answer