Why do we need to save data in R Data Format .......

When we save data / write data using R in .csv or .xls format , then 

Advantage :- This saved data can be used by any application other than R 

Disadvantage :- The R specific Data Structure or Data Type is lost ( meaning Factor column is not preserved ) 

Hence , the solution for this limitation is to save data in R Data format .
Also , saving data in R data format considerably reduces the size of the large files by applying compression.


There are 2 ways to save data from R in R Data Format ..

a) Save a single R data object using saveRDS() function [ it creates a *.rds file in your system ]
Example : Save a subset of mtcars data on your system as mtcars_mean_mpg.rds file
saveRDS(mtcars[mtcars$mpg > mean(mtcars$mpg) , ] , file="mtcars_mean_mpg.rds")

    Once data from R is stored in *.rds format , same can be read back into R while preserving all the data properties by using readRDS()

Example : Read the *.rds file back into R 
mtcars_mean_mpg <- readRDS(file="mtcars_mean_mpg.rds")

b) Save multiple R data objects collectively using save() function [ it creates a *.rda / *.rdata file on your system ]

Example : Save 2 data objects on your system as one *.rdata file on your system
mtcars_GT_Mean_Mpg <- mtcars[mtcars$mpg > mean(mtcars$mpg) , ]
mtcars_LT_Mean_Mpg <- mtcars[mtcars$mpg < mean(mtcars$mpg) , ]
save(mtcars_GT_Mean_Mpg,mtcars_LT_Mean_Mpg, file="mtcars_GT_LT_mean_mpg.rdata")

Note - Above R code will create a "mtcars_GT_LT_mean_mpg.rdata" file on your system 

Example : We can retrieve the saved data back from *.rdata file using load() function 

Note - Load function make the data objects available to R with the same Data Objects names which was used to create *.rdata file.

Thanks & Happy Learning 
Priyaranjan Mohanty
@AUTHOR : Admin

Tags:Eco, Water, Air, Environment

Comments (0)

    No Comments Found
Leave a Comment