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.
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.
Data Serialization (RDS) using R
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.
Contact Us