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.
Activity | Package | Function |
Read RData | base | load() |
Read RDS | base | readRDS() |
Write RData | base | save() |
Write RDS | base | saveRDS() |
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:
Or specify the full path of where the file is located:
The "testdata" object contained in the "testdata.RData" file can now be viewed:
You can also view the object(s) contained in the .RData file with the line below:
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:
Or specify the full path:
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):
Or specify the full path of where you want to save it:
The RData file should look like this reloaded in R and the objects viewed:
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:
The RDS file should look like this reloaded in R:
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes