Dear ALL: I am teaching statistical packages class this semester, in R programing I am trying to explain the use of save() and load() with an example using the iris data. It seems that the save() function works, BUT when I tried to load the data back to R, it seems that there is a problem(s), I could not figure out what went wrong. Any help would be highly appreciated. I saved the iris data in my computer in the text format, "iris.with.head.txt ". Here are my R codes:> irisdata<-read.table("G:/iris.with.head.txt", header=T) > > head(irisdata)Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa *##### saving the data as an .rda* save(irisdata,file="G:/irisdataTest.rda") *##### load the data back to R* load(file="G:/irisdataTest.rda")>head(irisdataTest)Error in head(irisdataTest) : object 'irisdataTest' not found> irisdataTestError: object 'irisdataTest' not found with many thanks abou ______________________ *AbouEl-Makarim Aboueissa, PhD* *Professor, Mathematics and Statistics* *Graduate Coordinator* *Department of Mathematics and Statistics* *University of Southern Maine* [[alternative HTML version deleted]]
Hi, You can try this: head(irisdata) Objects loaded by load() keep their names when being saved. In your case, it is 'irisdata'. You can also use verbose = TRUE to show the names of objects loaded: load(file = "irisdataTest.RData", verbose = TRUE) Hope this helps. Regards, Shu Fai On Tue, Sep 26, 2023 at 9:24?AM AbouEl-Makarim Aboueissa <abouelmakarim1962 at gmail.com> wrote:> > Dear ALL: > > I am teaching statistical packages class this semester, in R programing I > am trying to explain the use of save() and load() with an example using the > iris data. It seems that the save() function works, BUT when I tried to > load the data back to R, it seems that there is a problem(s), I could not > figure out what went wrong. > > Any help would be highly appreciated. > > > I saved the iris data in my computer in the text format, "iris.with.head.txt > ". > > Here are my R codes: > > > irisdata<-read.table("G:/iris.with.head.txt", header=T) > > > > head(irisdata) > Sepal.Length Sepal.Width Petal.Length Petal.Width Species > 1 5.1 3.5 1.4 0.2 setosa > 2 4.9 3.0 1.4 0.2 setosa > 3 4.7 3.2 1.3 0.2 setosa > 4 4.6 3.1 1.5 0.2 setosa > 5 5.0 3.6 1.4 0.2 setosa > 6 5.4 3.9 1.7 0.4 setosa > > > > *##### saving the data as an .rda* > > save(irisdata,file="G:/irisdataTest.rda") > > *##### load the data back to R* > > load(file="G:/irisdataTest.rda") > > > >head(irisdataTest) > Error in head(irisdataTest) : object 'irisdataTest' not found > > > irisdataTest > Error: object 'irisdataTest' not found > > > > with many thanks > abou > ______________________ > > > *AbouEl-Makarim Aboueissa, PhD* > > *Professor, Mathematics and Statistics* > *Graduate Coordinator* > > *Department of Mathematics and Statistics* > *University of Southern Maine* > > [[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-guide.html > and provide commented, minimal, self-contained, reproducible code.
You never created any object in R called irisdataTest. Objects in the global environment have names that are unrelated to the names of files on disk. The load function modifies an environment to create a variable named as it was named in the environment from which it was saved. Thus, you cannot simply load an object that was saved with one name into an object named something else. It is possible to create a new environment to put the loaded objects into, but I wouldn't recommend trying to explain how to do that to a beginner. Rather, I would instead recommend using saveRDS and readRDS instead to save/load exactly one object at a time without storing the object name. saveRDS( mtcars, "my_mtcars.rds" ) new_obj <- readRDS( "my_mtcars.rds" ) I would also guide them to never save their environment when prompted by R... the .RData file this creates will remember mistakes made in previous sessions making troubleshooting very difficult later. Instead they should focus on making a top-to-bottom script that has all their analysis steps so they can start from scratch. On September 25, 2023 6:23:01 PM PDT, AbouEl-Makarim Aboueissa <abouelmakarim1962 at gmail.com> wrote:>Dear ALL: > >I am teaching statistical packages class this semester, in R programing I >am trying to explain the use of save() and load() with an example using the >iris data. It seems that the save() function works, BUT when I tried to >load the data back to R, it seems that there is a problem(s), I could not >figure out what went wrong. > >Any help would be highly appreciated. > > >I saved the iris data in my computer in the text format, "iris.with.head.txt >". > >Here are my R codes: > >> irisdata<-read.table("G:/iris.with.head.txt", header=T) >> >> head(irisdata) > Sepal.Length Sepal.Width Petal.Length Petal.Width Species >1 5.1 3.5 1.4 0.2 setosa >2 4.9 3.0 1.4 0.2 setosa >3 4.7 3.2 1.3 0.2 setosa >4 4.6 3.1 1.5 0.2 setosa >5 5.0 3.6 1.4 0.2 setosa >6 5.4 3.9 1.7 0.4 setosa > > > >*##### saving the data as an .rda* > >save(irisdata,file="G:/irisdataTest.rda") > >*##### load the data back to R* > >load(file="G:/irisdataTest.rda") > > >>head(irisdataTest) >Error in head(irisdataTest) : object 'irisdataTest' not found > >> irisdataTest >Error: object 'irisdataTest' not found > > > >with many thanks >abou >______________________ > > >*AbouEl-Makarim Aboueissa, PhD* > >*Professor, Mathematics and Statistics* >*Graduate Coordinator* > >*Department of Mathematics and Statistics* >*University of Southern Maine* > > [[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-guide.html >and provide commented, minimal, self-contained, reproducible code.-- Sent from my phone. Please excuse my brevity.