Saving objects in RData file in different name

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Saving objects in RData file in different name

Bogaso
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Saving objects in RData file in different name

K. Elo-2
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]]
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Saving objects in RData file in different name

Jeremie Juste
In reply to this post by Bogaso
Christofer Bogaso <[hidden email]> 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Saving objects in RData file in different name

Ivan Calandra-5
In reply to this post by K. Elo-2
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]]
>>
>> ______________________________________________
>> [hidden email] 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.
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.