In previous articles, we described the essentials of R programming and provided quick start guides for reading and writing txt and csv files using R base functions as well as using a most modern R package named readr, which is faster (X10) than R base functions. We also described different ways for reading and writing Excel files in R.
Writing data, in txt, csv or Excel file formats, is the best solution if you want to open these files with other analysis software, such as Excel. However this solution doesn’t preserve data structures, such as column data types (numeric, character or factor). In order to do that, the data should be written out in R data format.
Saving data into R data formats can reduce considerably the size of large files by compression.
Launch RStudio as described here: Running RStudio and setting up your working directory
Save one object to a file
It’s possible to use the function saveRDS() to write a single R object to a specified file (in rds file format). The object can be restored back using the function readRDS().
Note that, it’s possible to restore the object under a different name
The simplified syntax for saving and restoring is as follow:
# Save an object to a file saveRDS(object, file = "my_data.rds") # Restore the object readRDS(file = "my_data.rds")
- object: An R object to save
- file: the name of the file where the R object is saved to or read from
In the R code below, we’ll save the mtcars data set and restore it under different name:
# Save a single object to a file saveRDS(mtcars, "mtcars.rds") # Restore it under a different name my_data <- readRDS("mtcars.rds")
Save multiple objects to a file
The function save() can be used to save one or more R objects to a specified file (in .RData or .rda file formats). The function can be read back from the file using the function load().
Note that if you save your data with save(), it cannot be restored under different name. The original object names are automatically used.
# Saving on object in RData format save(data1, file = "data.RData") # Save multiple objects save(data1, data2, file = "data.RData") # To load the data again load("data.RData")
Save your entire workspace
It’s a good idea to save your workspace image when your work sessions are long.
This can be done at any time using the function save.image()
That stores your workspace to a file named .RData by default. This will ensure you don’t lose all your work in the event of system reboot, for instance.
When you close R/RStudio, it asks if you want to save your workspace. If you say yes, the next time you start R that workspace will be loaded. That saved file will be named .RData as well.
It’s also possible to specify the file name for saving your work space:
save.image(file = "my_work_space.RData")
To restore your workspace, type this:
Save and restore one single R object: saveRDS(object, file), my_data <- readRDS(file)
Save and restore multiple R objects: save(data1, data2, file = “my_data.RData”), load(“my_data.RData”)
- Save and restore your entire workspace: save.image(file = “my_work_space.RData”), load(“my_work_space.RData”)
This analysis has been performed using R (ver. 3.2.3).
Want to Learn More on R Programming and Data Science?
Follow us by Email On Social Networks: