How to Save and Load RDA Files in R (With Examples)


Files that end with an .rda extension represent Rdata files.

You can use the save() function to save these types of files in R:

save(df, file='my_data.rda')

And you can use the load() function to load these types of files in R:

load(file='my_data.rda')

The following example shows how to use each of these functions in practice.

Example: Save and Load RDA Files in R

Suppose we create the following data frame in R:

#make this example reproducible
set.seed(0)

#create data frame
df <- data.frame(x=rnorm(100),
                 y=rnorm(100),
                 z=rnorm(100))

#view data frame
head(df)

           x          y          z
1  1.2629543  0.7818592 -1.0457177
2 -0.3262334 -0.7767766 -0.8962113
3  1.3297993 -0.6159899  1.2693872
4  1.2724293  0.0465803  0.5938409
5  0.4146414 -1.1303858  0.7756343
6 -1.5399500  0.5767188  1.5573704

We can use the save() function to save this data frame to an .rda file:

This file will automatically be saved in the current working directory. You can find the working directory by using the getwd() function:

#display working directory
getwd()

"C:/Users/Bob/Documents"

Now suppose we use the rm() function to remove the data frame from the current R environment:

#remove data frame from current R environment
rm(df)

If we look at our current environment in RStudio, we’ll see that it doesn’t contain any objects:

We can then use the load() function to load the .rda file into the current R environment:

load(file='my_data.rda')

If we look at the current environment again in RStudio, we’ll see that it now contains the data frame:

Additional Resources

The following tutorials explain how to read other types of files in R:

How to Import CSV Files into R
How to Import Excel Files into R
How to Import TSV Files into R

Leave a Reply

Your email address will not be published.