How to Save a Data Frame in R (Example)
In this article, I’ll show how to save a data frame in R programming.
Table of contents:
Let’s dive right into the examples…
Introduction of Example Data
In the example of this tutorial, we’ll use the following data frame in R:
data <- data.frame(x1 = 1:5, # Create example data x2 = letters[1:5]) data # Print example data # x1 x2 # 1 a # 2 b # 3 c # 4 d # 5 e
Our example data contains two columns and five rows.
Example: Save Data Frame in R
If we want to export our example data frame to our computer, we first need to set the working directory, to which we want to save the data:
setwd("C:/Users/... Your Path .../Desktop/") # Set working directory
Then, we can use the save function to store the data frame on our computer:
save(data, file = "my_data_frame.Rda") # Save data frame
Run the previous R code and then have a look at your currently used working directory. You’ll find an Rda object with the name my_data_frame there.
If we want to load our data frame at a later point in time, we can use the load function:
load(file = "my_data_frame.Rda") # Reload data frame
Video, Further Resources & Summary
Some time ago I have published a video on my YouTube channel, which shows the R syntax of this article. Please find the video below:
In addition, you might want to read the related tutorials on this website. A selection of articles is listed below:
- Getting & Setting Working Directories
- Save & Load RData Workspace Files in R
- The R Programming Language
Summary: At this point you should have learned how to export and store a data frame in R programming. In case you have additional questions, let me know in the comments below.