Christofer Bogaso
2018-Jul-28 11:34 UTC
[R] Saving objects in RData file in different name
Hi, Let say I have 2 objects as below x1 = 1:3 x2 = 5:4 Now I want to save both x1 and x2 in some RData file, however x1 will be saved with a different name e.g. y I tried below save(y = x1, x2, file = "file.RData") However still they are saved in their original names i.e. x1 and x2, not y and y2. Is there any possibility that I can achieve above without explicitly copying y = x1 before y is passed to save() Thanks for any feedback. [[alternative HTML version deleted]]
Hi! Maybe not the most elegant solution, but a workaround is to have a function:> save2<-function(y, ...) { save(y,...)} > save2(x1,x2,file="test.RData")The point is to include the variables to be "renamed" as parameters (in my example: y). The function will use the parameter variable names when saving the file. HTH, Kimmo 2018-07-28, 17:04 +0530, Christofer Bogaso wrote:> Hi, > > Let say I have 2 objects as below > > x1 = 1:3 > x2 = 5:4 > > Now I want to save both x1 and x2 in some RData file, however x1 will > be > saved with a different name e.g. y > > I tried below > > save(y = x1, x2, file = "file.RData") > > However still they are saved in their original names i.e. x1 and x2, > not y > and y2. > > Is there any possibility that I can achieve above without explicitly > copying y = x1 before y is passed to save() > > Thanks for any feedback. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-gui > de.html > and provide commented, minimal, self-contained, reproducible code.
Christofer Bogaso <bogaso.christofer at gmail.com> writes: Hello In case you have conflicting data issue when you load data, you can also do it the other way around. Indeed you never know when a possible conflict might occur in the future. The following line load the data in a new environment e first then get it back to your current environment. loaded..data <- get(load("test.RData",e<- new.env()),e) you might then want to remove everything in that enviromnent afterwards, rm(list=ls(envir=e1), envir=e1) HTH Jeremie
Hi! In those cases, I use R.utils::saveObject() and loadObject(). You would have to save each object separately though: saveObject(x1, file="file.Rbin") y <- loadObject(file="file.Rbin") HTH Ivan -- Dr. Ivan Calandra TraCEr, laboratory for Traceology and Controlled Experiments MONREPOS Archaeological Research Centre and Museum for Human Behavioural Evolution Schloss Monrepos 56567 Neuwied, Germany +49 (0) 2631 9772-243 https://www.researchgate.net/profile/Ivan_Calandra On 28/07/2018 14:36, K. Elo wrote:> Hi! > > Maybe not the most elegant solution, but a workaround is to have a > function: > >> save2<-function(y, ...) { save(y,...)} >> save2(x1,x2,file="test.RData") > The point is to include the variables to be "renamed" as parameters (in > my example: y). The function will use the parameter variable names when > saving the file. > > HTH, > Kimmo > > 2018-07-28, 17:04 +0530, Christofer Bogaso wrote: >> Hi, >> >> Let say I have 2 objects as below >> >> x1 = 1:3 >> x2 = 5:4 >> >> Now I want to save both x1 and x2 in some RData file, however x1 will >> be >> saved with a different name e.g. y >> >> I tried below >> >> save(y = x1, x2, file = "file.RData") >> >> However still they are saved in their original names i.e. x1 and x2, >> not y >> and y2. >> >> Is there any possibility that I can achieve above without explicitly >> copying y = x1 before y is passed to save() >> >> Thanks for any feedback. >> >> [[alternative HTML version deleted]] >> >> ______________________________________________ >> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide http://www.R-project.org/posting-gui >> de.html >> and provide commented, minimal, self-contained, reproducible code. > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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. >