How to read and write RData or RDS files in R or import data from and export data to RData or RDS files in R, using load(), readRDS(), save() and saveRDS().

The functions are from the "base" package, hence, you do not need to do any installation.

Packages and Functions for Reading and Writing RData and RDS Files in R
Activity Package Function
Read RData base load()
Read RDS base readRDS()
Write RData base save()
Write RDS base saveRDS()

1 Read or Import Data from RData Files in R

Download testdata.RData file.

The RData file format is used to store multiple R objects within a single file.

To read or import .RData files in R, if you set or have the working directory as the folder containing the file, use the line of code below:

load("testdata.RData")

Or specify the full path of where the file is located:

load("C:/Users/Public/Statscodes/Rdata/read-files/testdata.RData")

The "testdata" object contained in the "testdata.RData" file can now be viewed:

testdata
RData File Read in R

RData File Read in R

You can also view the object(s) contained in the .RData file with the line below:

str(load("C:/Users/Public/Statscodes/Rdata/read-files/testdata.RData"))
Image Showing Objects in RData File

Image Showing Objects in RData File

2 Read or Import Data from RDS Files in R

Download testdata.RDS file.

The RDS file format can be used to store a single dataframe and is usually smaller in size compared to a text file.

To read or import .RDS files in R, if the file is in your working directory, you can use the line below:

readRDS("testdata.RDS")

Or specify the full path:

readRDS("C:/Users/Public/Statscodes/Rdata/read-files/testdata.RDS")
RDS File Read in R

RDS File Read in R

3 Write or Export Data to RData and RDS Files in R

The dataframe named dtfrm will be used:

dtfrm = data.frame(Group = c("A", "B", "B", "C", "D"), 
                   ID = c("A02", "B12", "B15", "C04", "D07"), 
                   Score = c(9, 8, 8, 10, 7), 
                   Position = c(2, 3, 3, 1, 5))
dtfrm
  Group  ID Score Position
1     A A02     9        2
2     B B12     8        3
3     B B15     8        3
4     C C04    10        1
5     D D07     7        5

To write or export data to .RData files in R including one or more objects, use the line of code below to save the .RData file to the working directory (note that here, we are writing two dataframe objects):

save(dtfrm, BOD, file = "outdata.RData")

Or specify the full path of where you want to save it:

save(dtfrm, BOD, file = "C:/Users/Public/Statscodes/Rdata/write-files/outdata.RData")

The RData file should look like this reloaded in R and the objects viewed:

RData File Written and Reloaded in R

RData File Written and Reloaded in R

To write or export data to .RDS files in R, use the line of code below to save the .RDS file to the working directory:

saveRDS(dtfrm, file = "outdata.RDS")

The RDS file should look like this reloaded in R:

RDS File Written and Reloaded in R

RDS File Written and Reloaded in R

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes