On 15/09/16 14:04, S Ellison wrote:
If you want
to add variable to data.frame you have to use attach, detach. Right?
I'd have said "not at all", not "not quite". attach and detach have
almost exactly nothing to do with adding to a data frame. You can add
to a data frame using dfrm$newvar <- <something> dfrm['newvar'] <-
<something> cbind(dfrm, newvar=<something>) #adds a new variable
called 'newvar' rbind #to add rows merge #to add columns and/or rows
from another data frame ... and a few other things.
The only relevance of attach/detach is to do with the behaviour of
attached objects, not to do with adding to data frames. If you have
attach()ed something, changing the original object does not
automatically update the copy of its variables in the current
environment, or vice versa, because attach(), as documented, creates
a _copy_. So _if_ you have attach()ed a data frame - or a list - you
can't change the copy by changing the original object and you can't
change the original object by changing the copy. Only if you need to
change both do you need to detach and reattach.
As a rule, I generally avoid attach() for that and other reasons
(most of which are listed in ?attach). attach()is only sensible if
you have already completed all the manipulation needed on the
attached object first. Even then, using with() is safer.
Extremely well and clearly put. This is one of those "I wish *I* had
said that!" posts.
cheers,
Rolf
--
Technical Editor ANZJS
Department of Statistics
University of Auckland
Phone: +64-9-373-7599 ext. 88276
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.