[R] Odp: Problem with retrieving updated variables after attach()

Petr PIKAL petr.pikal at precheza.cz
Tue Feb 10 14:27:40 CET 2009


Hi

r-help-bounces at r-project.org napsal dne 10.02.2009 13:57:44:

> G'day David,
> 
> On Tue, 10 Feb 2009 13:23:50 +0100
> David Croll <david.croll at gmx.ch> wrote:
> 
> > Well, I knew that attach() only creates a copy of the variables in
> > the search path.
> > 
> > What I wanted to ask was how to *retrieve* that copy...
> > 
> >  > mat
> >  id age
> > 1 NA  NA
> > 2 NA  NA
> > 3 NA  NA
> > 4 NA  NA
> > 5 NA  NA 
> > 
> >  > attach(mat)
> > 
> >  > id <<- sample(100,5)
> >  > age <<- rnorm(5,mean=30)
> > 
> > How can I make a new data frame out of the id and age that were
> > changed above?
> 
> R> new.df <- data.frame(id,age)
> 
> Or, perhaps more automatic:
> 
> R> new.df <- do.call(data.frame, sapply(ls(pos=2), as.name))
> 
> Is this what you are after?

The question is why he wants to do this? I do not see the point of 
attaching data frame, changing something in attached frame and then 
keeping a new copy.

Why not

mat
new.mat <- mat
new.mat[,n1] <- some change
new.mat[,n2] <- some other change

without tricky use of attach if he wants to keep the changed copy.

Regards
Petr

> 
> Cheers,
> 
>    Berwin
> 
> =========================== Full address =============================
> Berwin A Turlach                            Tel.: +65 6516 4416 (secr)
> Dept of Statistics and Applied Probability        +65 6516 6650 (self)
> Faculty of Science                          FAX : +65 6872 3919 
> National University of Singapore 
> 6 Science Drive 2, Blk S16, Level 7          e-mail: statba at nus.edu.sg
> Singapore 117546                    http://www.stat.nus.edu.sg/~statba
> 
> ______________________________________________
> 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