[R] why data.frame, mutate package and not lists

Rolf Turner r.turner at auckland.ac.nz
Thu Sep 15 14:24:48 CEST 2016


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



More information about the R-help mailing list