4.5 C
London
Thursday, December 19, 2024
HomeRImport & Export Data in RHow to Save and Load RDA Files in R (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories