David Croll
2009-Feb-10 11:02 UTC
[R] Problem with retrieving updated variables after attach()
Hello, suppose I have a data frame:> matid age 1 NA NA 2 NA NA 3 NA NA 4 NA NA 5 NA NA Then I attach the data frame:> attach(mat)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 which still are attached to position 2 of the R environment...> new_mat <- data.frame(ls(pos=2)) # I want to rescue ALL variables that were created by attach(mat)> new_matls.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 -- Jetzt 1 Monat kostenlos! GMX FreeDSL - Telefonanschluss + DSL f?r nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a
Petr PIKAL
2009-Feb-10 11:42 UTC
[R] Odp: Problem with retrieving updated variables after attach()
Hi r-help-bounces at r-project.org napsal dne 10.02.2009 12:02:53:> Hello, > > suppose I have a data frame: > > > mat > id age > 1 NA NA > 2 NA NA > 3 NA NA > 4 NA NA > 5 NA NA > > Then I attach the data frame: > > > attach(mat)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 agewhich> still are attached to position 2 of the R environment... > > > new_mat <- data.frame(ls(pos=2)) # I want to rescue ALL variables thatwere> created by attach(mat) > > > new_mat > ls.pos...2. > 1 age > 2 id > > But this leads to a bogus object... how can I rescue the updated id andage> values into new_mat?What about not using attach and transform mat directly> mat$age <- rnorm(5,mean=30) > mat$id <- sample(100,5) > matid age 1 24 29.17842 2 88 31.22606 3 32 30.81540 4 5 29.31528 5 11 29.32775 Regards Petr> > > Regards, > > David > > -- > Jetzt 1 Monat kostenlos! GMX FreeDSL - Telefonanschluss + DSL > f?r nur 17,95 Euro/mtl.!* http://dsl.gmx.de/?ac=OM.AD.PD003K11308T4569a > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Wacek Kusnierczyk
2009-Feb-10 12:01 UTC
[R] Problem with retrieving updated variables after attach()
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:> Hello, > > suppose I have a data frame: > > >> mat >> > id age > 1 NA NA > 2 NA NA > 3 NA NA > 4 NA NA > 5 NA NA > > Then I attach the data frame: > > >> attach(mat) >> > > 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 which still are attached to position 2 of the R environment... > > >> new_mat <- data.frame(ls(pos=2)) # I want to rescue ALL variables that were created by attach(mat) >> > > >> new_mat >> > 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? >