Skip to content

Problem with retrieving updated variables after attach()

10 messages · David Croll, Berwin A Turlach, Peter Dalgaard +2 more

#
Hello,

suppose I have a data frame:
id age
1 NA  NA
2 NA  NA
3 NA  NA
4 NA  NA
5 NA  NA

Then I attach the data frame:
I assign some new values...
Then I want to create a new data frame from the variables id and age which still are attached to position 2 of the R environment...
ls.pos...2.
1         age
2          id

But this leads to a bogus object... how can I rescue the updated id and age values into new_mat?


Regards,

David
#
Hi


r-help-bounces at r-project.org napsal dne 10.02.2009 12:02:53:
Look into docs what attach does. If you do not understand environments use 
attach with great care

The database is not actually attached. Rather, a new environment is 
created on the search path and the elements of a list (including columns 
of a data frame) or objects in a save file or an environment are copied 
into the new environment. If you use <<- or assign to assign to an 
attached database, you only alter the attached copy, not the original 
object. (Normal assignment will place a modified version in the user's 
workspace: see the examples.) For this reason attach can lead to 
confusion.
which
were
age
What about not using attach and transform mat directly
id      age
1 24 29.17842
2 88 31.22606
3 32 30.81540
4  5 29.31528
5 11 29.32775

Regards
Petr
http://www.R-project.org/posting-guide.html
#
attach provides a copy of rather than aliases to the variables within a
data frame.

d = data.frame(x=0)
attach(d)

x
# 0, from the attached copy of d

x = 1
x
# 1, from the global anvironment
d$x
# 0, from d

x <<- 2
x
# 1, from the global environment
d$x
# 0, from d
get('x', pos=2)
# 2, from the attached copy of d

rm(x)
x
# 2, from the attached copy of d

vQ
David Croll 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?
#
David Croll wrote:
data.frame(id, age) # from the copy
data.frame(mat$id, mat$age) # from the originals

why do you need to overwrite the copy?

vQ
#
G'day David,

On Tue, 10 Feb 2009 13:23:50 +0100
David Croll <david.croll at gmx.ch> wrote:

            
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?

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
#
Berwin A Turlach wrote:
There is also

as.data.frame(as.list(pos.to.env(2)))

or ...as.environment("mat")

I wouldn't recommend using <<- like that though; the consequences of
spelling mistakes on the LHS are a bit scary. If you must, consider

with(pos.to.env(2),{
  id <- sample(100,5)
  age <- rnorm(5,mean=30)
})

or better, modify before attach, using within() or transform().
#
Berwin A Turlach wrote:
or perhaps:

new.df <- as.data.frame(mget(names(mat), as.environment(2)))

vQ
#
Hi

r-help-bounces at r-project.org napsal dne 10.02.2009 13:57:44:
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
http://www.R-project.org/posting-guide.html
#
Thank you, this does the job perfectly!


Regards, David