Hi
r-help-bounces at r-project.org napsal dne 10.02.2009 12:02:53:
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:
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.
I assign some new values...
id <<- sample(100,5)
age <<- rnorm(5,mean=30)
Then I want to create a new data frame from the variables id and age
still are attached to position 2 of the R environment...
new_mat <- data.frame(ls(pos=2)) # I want to rescue ALL variables that
ls.pos...2.
1 age
2 id
But this leads to a bogus object... how can I rescue the updated id and
What about not using attach and transform mat directly
mat$age <- rnorm(5,mean=30)
mat$id <- sample(100,5)
mat
id age
1 24 29.17842
2 88 31.22606
3 32 30.81540
4 5 29.31528
5 11 29.32775
Regards
Petr