Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data Visualization
  • Statistics in R
  • Machine Learning in R
  • Data Science in R
  • Packages in R
  • Data Types
  • String
  • Array
  • Vector
  • Lists
  • Matrices
  • Oops in R
Open In App
Next Article:
Data Serialization (RDS) using R
Next article icon

Data Serialization (RDS) using R

Last Updated : 28 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we can learn the Data Serialization using R. In R, one common serialization method is to use the RDS (R Data Serialization) format.

Data Serialization (RDS) using R

Data serialization is the process of converting data structures or objects into a format that can be easily stored, transmitted, or reconstructed later. In R Programming Language one common method for data serialization is to use the RDS (R Data Serialization) format. The RDS format allows us to save R objects, such as data frames or models, to a file and later read them back into R.

  • Serialization: The process of converting complex data structures into an understandable format, suitable for storage and transmission is known as Serialization.

Significance of Data Seralization:

  1. Data Preservation: It's necessary to keep your objects' class properties and structure as it is while working with complex data structures in R. It is possible because serialization promises data's integrity will not be compromised during deserialization, or "unpacking."
  2. Data share: It's normal for distinct applications or systems to need to share data. Data sharing between platforms is made simple by serialization, that gives a uniform format independent of computer language.
  3. Storage Efficiency: Data stored in human-readable text forms like CSV or JSON takes less space than data stored in serialization formats like RDS. When working with big datasets, it might be very crucial.
  4. Diminished Data Transfer Overhead: Data that has been serialized can cut down on the overhead that goes with translating data into and out of different formats via networks. The result of this is reduced resource use and quicker data transmission.

Basic Concepts in RDS

  1. RDS (R Data Serialization):
    • RDS is a binary serialization format in R used to save R objects to a file.
    • It allows you to save and load R objects while preserving their class, attributes, and structure.
  2. Serialization Functions:
    • saveRDS() function in R is used to serialize an R object to a file.
    • readRDS() function in R is used to deserialize and read the R object back into the R environment.
  3. Saving and Loading Data: Use saveRDS() to save R objects to a file, and readRDS() to load them back into R.
  4. Serialization of Different Data Types:
    • RDS can serialize various data types, including vectors, lists, data frames, and more.
    • It's suitable for saving individual objects or entire datasets.
  5. Alternative Formats:
    • Besides RDS, other serialization formats like CSV, JSON, and Feather may be used based on specific requirements.
    • Choose the format that best fits the use case in terms of performance, interoperability, and storage size.
  6. Compressing Serialized Data: For large datasets, consider compressing serialized data to reduce file size. RDS supports compression using the "gzip" or "xz" compression algorithms.

Serializing and Deserializing | using saveRDS(), readRDS() functions:

  • Step 1: Serialize 'R' Object to RDS File
  • Step 2: Deserialize RDS File to R Object

Key Concepts:

  1. Serialization: Serialization converts data objects into a specific format that is storable or transmissible. In R, this is often done using the saveRDS() function.
  2. Deserialization: The reverse process, where using the readRDS() function, serialized data is converted back into its original R data structure.
  3. RDS File Format: RDS files with extension .RDS are binary files that store serialized R objects. Compared to standard text formats like CSV, it is more space-efficient.

Serialize and Deserialize a Data Frame

A key part in programming is data serialization, which enables us to store, transfer, and rebuild easily readable format from a complexed data structures. The .RDS file format is frequently used in the R programming community while seralization. Distributing or storing data for prior use is made easier by this format, which allows us to store R objects while balancing their class properties and structure. Let's discuss the fundamental ideas, procedures, and several instances of data serialization using RDS in this article.

Serializing and Deserializing a List

R
# Creating a list my_list <- list(numbers = 1:5, colors = c("red", "blue", "green"))  # Save the list to a file saveRDS(my_list, "serialized_list.rds")  # Read the serialized list back into R loaded_list <- readRDS("serialized_list.rds")  # Display the loaded list print(loaded_list) 

Output:

$numbers [1] 1 2 3 4 5  $colors [1] "red"   "blue"  "green"

Serialize a List of Data Frames

R
# Create two data frames df1 <- data.frame(Name = c("Ram", "Mina", "Sonu"), Age = c(32, 22, 24)) df2 <- data.frame(City = c("India", "Africa", "Japan"),                    Population = c(8398456, 2350456, 2765494))  # Create a list of data frames list_of_dfs <- list(data_frame1 = df1, data_frame2 = df2)  # Serialize the list of data frames saveRDS(list_of_dfs, file = "list_of_data_frames.RDS")  # Deserialize the list of data frames loaded_list_of_dfs <- readRDS("list_of_data_frames.RDS")  # Access and print one of the data frames print(loaded_list_of_dfs$data_frame1) print(loaded_list_of_dfs$data_frame2) 

Output:

  Name Age 1  Ram  32 2 Mina  22 3 Sonu  24      City Population 1  India    8398456 2 Africa    2350456 3  Japan    2765494 

Serialize a Custom R Object

R
# Create a custom R object custom_object <- structure(list(   name = "Minakshi",   age = 22,   city = "Bihar",   hobbies = c("Reading", "Writing", "Cooking"),   scores = c(math = 95, science = 89, history = 75) ))  # Serialize the custom object saveRDS(custom_object, file = "custom_object.RDS")  # Deserialize the custom object loaded_custom_object <- readRDS("custom_object.RDS")  # Access and print the loaded custom object print(loaded_custom_object) 

Output:

$name [1] "Minakshi"  $age [1] 22  $city [1] "Bihar"  $hobbies [1] "Reading" "Writing" "Cooking"  $scores    math science history       95      89      75  

We can see our save file in with the same name that we have given so we can access this file any time when its required.


ghj
Data Serialization (RDS) using r


This process is particularly useful for saving and sharing R objects, especially when the data or objects are too large to be easily shared in code form.

Keep in mind that while RDS is a convenient format for saving and loading R objects, it is specific to R. If you need to exchange data with other programming languages, you might want to consider other formats like CSV, JSON, or binary formats that are more widely supported.


Next Article
Data Serialization (RDS) using R

M

manojjai07nu
Improve
Article Tags :
  • R Language
  • Geeks Premier League
  • Geeks Premier League 2023

Similar Reads

    Read XML Data with rvest using R
    XML stands for Extensible Markup Language, XML is just wrapping information in tags. It is a widely used markup language designed for storing and transporting data in a structured and human-readable format. It is designed to carry data and only focus on "what data is". It is a text-based format that
    6 min read
    Manipulate R Data Frames Using SQL
    Manipulating data frames in R Programming using SQL can be easily done using the sqldf package. This package in R provides a mechanism that allows data frame manipulation with SQL and also helps to connect with a limited number of databases. The sqldf package in R is basically used for executing the
    8 min read
    Web Scraping using R Language
    Web scraping is a technique that allows us to automatically extract information from websites, in situations where the data we need isn’t available through downloadable datasets or public APIs (Application Programming Interfaces). Instead of manually copying and pasting content, web scraping uses co
    4 min read
    Scrape an HTML Table Using rvest in R
    Web scraping is a technique used to extract data from websites. In R, the rvest package is a popular tool for web scraping. It's easy to use and works well with most websites. This article helps you to process of scraping an HTML table using rvest.What is rvest?rvest is an R package that simplifies
    6 min read
    Data Preprocessing in R
    Data preprocessing is an important step in data analysis and machine learning. In R, we use various tools to clean, manipulate and prepare data for analysis. In this article we will explore the essential steps involved in data preprocessing using R.1. Installing and Loading Required PackagesThe tidy
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences