Saving Data into R Data Format: RDS and RDATA
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.
Preleminary tasks
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()
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:
load("my_work_space.RData")
Summary
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”)
Infos
This analysis has been performed using R (ver. 3.2.3).
Show me some love with the like buttons below... Thank you and please don't forget to share and comment below!!
Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!
Recommended for You!
Recommended for you
This section contains the best data science and self-development resources to help you on your path.
Books - Data Science
Our Books
- Practical Guide to Cluster Analysis in R by A. Kassambara (Datanovia)
- Practical Guide To Principal Component Methods in R by A. Kassambara (Datanovia)
- Machine Learning Essentials: Practical Guide in R by A. Kassambara (Datanovia)
- R Graphics Essentials for Great Data Visualization by A. Kassambara (Datanovia)
- GGPlot2 Essentials for Great Data Visualization in R by A. Kassambara (Datanovia)
- Network Analysis and Visualization in R by A. Kassambara (Datanovia)
- Practical Statistics in R for Comparing Groups: Numerical Variables by A. Kassambara (Datanovia)
- Inter-Rater Reliability Essentials: Practical Guide in R by A. Kassambara (Datanovia)
Others
- R for Data Science: Import, Tidy, Transform, Visualize, and Model Data by Hadley Wickham & Garrett Grolemund
- Hands-On Machine Learning with Scikit-Learn, Keras, and TensorFlow: Concepts, Tools, and Techniques to Build Intelligent Systems by Aurelien Géron
- Practical Statistics for Data Scientists: 50 Essential Concepts by Peter Bruce & Andrew Bruce
- Hands-On Programming with R: Write Your Own Functions And Simulations by Garrett Grolemund & Hadley Wickham
- An Introduction to Statistical Learning: with Applications in R by Gareth James et al.
- Deep Learning with R by François Chollet & J.J. Allaire
- Deep Learning with Python by François Chollet
Click to follow us on Facebook :
Comment this article by clicking on "Discussion" button (top-right position of this page)