Hello, I created an array to hold the results of a series of simulations I'm running: d.eta <- array(0,dim=c(3,3,200)) <simulation goes here and populates the array but it's not important> Then I tried to save the results using this: save(d.eta,file="D:/Simulation Results/sim 9-23-11 deta") When I later tried to reload them using this: d.eta <- load(file="D:/Simulation Results/sim 9-23-11 deta") I got the following:> class(d.eta)[1] "character"> d.eta[1] "d.eta" Why didn't it load the original object that I tried to save (the array)? Is the problem with how I'm saving or how I'm loading? Any explanation would be greatly appreciated. And to head off this question, I did check after the simulation, before saving, and the d.eta object is an array of numbers. Thanks, Mitch
A more compact example might be helpful: g <- array(0,dim=c(4,4)) g save(g,file="D:/g") h <- load(file="D:/g") h -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Downey, Patrick Sent: Friday, September 23, 2011 9:32 AM To: r-help at r-project.org Subject: [R] 'save' saved object names instead of objects Hello, I created an array to hold the results of a series of simulations I'm running: d.eta <- array(0,dim=c(3,3,200)) <simulation goes here and populates the array but it's not important> Then I tried to save the results using this: save(d.eta,file="D:/Simulation Results/sim 9-23-11 deta") When I later tried to reload them using this: d.eta <- load(file="D:/Simulation Results/sim 9-23-11 deta") I got the following:> class(d.eta)[1] "character"> d.eta[1] "d.eta" Why didn't it load the original object that I tried to save (the array)? Is the problem with how I'm saving or how I'm loading? Any explanation would be greatly appreciated. And to head off this question, I did check after the simulation, before saving, and the d.eta object is an array of numbers. Thanks, Mitch ______________________________________________ R-help at r-project.org mailing list 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.
Jean-Christophe BOUËTTÉ
2011-Sep-23 13:43 UTC
[R] 'save' saved object names instead of objects
Hi, did you try load(file="D:/Simulation Results/sim 9-23-11 deta") without the assignment ? look at ?load 2011/9/23 Downey, Patrick <PDowney at urban.org>:> Hello, > > I created an array to hold the results of a series of simulations I'm > running: > > d.eta <- array(0,dim=c(3,3,200)) > > <simulation goes here and populates the array but it's not important> > > Then I tried to save the results using this: > > save(d.eta,file="D:/Simulation Results/sim 9-23-11 deta") > > When I later tried to reload them using this: > > d.eta <- load(file="D:/Simulation Results/sim 9-23-11 deta") > > I got the following: > >> class(d.eta) > [1] "character" >> d.eta > [1] "d.eta" > > Why didn't it load the original object that I tried to save (the array)? Is > the problem with how I'm saving or how I'm loading? Any explanation would > be greatly appreciated. > > And to head off this question, I did check after the simulation, before > saving, and the d.eta object is an array of numbers. > > Thanks, > Mitch > > ______________________________________________ > R-help at r-project.org mailing list > 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. >
This is one of the rare cases in R where you don't want to save the return value. You loaded d.eta, and then promptly overwrote it with the return value, which is just the name of the object.> ls()character(0)> d.eta <- array(0,dim=c(3,3,200)) > dim(d.eta)[1] 3 3 200> save(d.eta, file="deta") > rm(d.eta) > ls()character(0)> load("deta") > ls()[1] "d.eta"> dim(d.eta)[1] 3 3 200> # and compare > rm(d.eta) > ls()character(0)> d.eta.name <- load("deta") > ls()[1] "d.eta" "d.eta.name"> dim(d.eta)[1] 3 3 200> d.eta.name[1] "d.eta">Sarah On Fri, Sep 23, 2011 at 9:31 AM, Downey, Patrick <PDowney at urban.org> wrote:> Hello, > > I created an array to hold the results of a series of simulations I'm > running: > > d.eta <- array(0,dim=c(3,3,200)) > > <simulation goes here and populates the array but it's not important> > > Then I tried to save the results using this: > > save(d.eta,file="D:/Simulation Results/sim 9-23-11 deta") > > When I later tried to reload them using this: > > d.eta <- load(file="D:/Simulation Results/sim 9-23-11 deta") > > I got the following: > >> class(d.eta) > [1] "character" >> d.eta > [1] "d.eta" > > Why didn't it load the original object that I tried to save (the array)? Is > the problem with how I'm saving or how I'm loading? Any explanation would > be greatly appreciated. > > And to head off this question, I did check after the simulation, before > saving, and the d.eta object is an array of numbers. > > Thanks, > Mitch >-- Sarah Goslee http://www.functionaldiversity.org
On 23/09/2011 9:31 AM, Downey, Patrick wrote:> Hello, > > I created an array to hold the results of a series of simulations I'm > running: > > d.eta<- array(0,dim=c(3,3,200)) > > <simulation goes here and populates the array but it's not important> > > Then I tried to save the results using this: > > save(d.eta,file="D:/Simulation Results/sim 9-23-11 deta") > > When I later tried to reload them using this: > > d.eta<- load(file="D:/Simulation Results/sim 9-23-11 deta") > > I got the following: > > > class(d.eta) > [1] "character" > > d.eta > [1] "d.eta" > Why didn't it load the original object that I tried to save (the array)?It did, and then it overwrote it with the result of load(). (Load recreates variables with their original names. The return value is a vector of names. Use saveRDS and readRDS if you want the value saved/restored without its name. Duncan Murdoch> Is > the problem with how I'm saving or how I'm loading? Any explanation would > be greatly appreciated. > > And to head off this question, I did check after the simulation, before > saving, and the d.eta object is an array of numbers. > > Thanks, > Mitch > > ______________________________________________ > R-help at r-project.org mailing list > 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.