In this article, we will show you how to save objects in R by the save()
function. The save()
function is built in R to help you save the R objects. Let’s learn more about the definition, the syntax of the save()
function, and how to save objects with the explanation and examples below.
Save objects in R
What does the save function do in R?
The save()
function in R is used to help you save the R objects to the specified file on your computer. By this way, you can reuse these objects in the future by reading back these objects from the file you saved by the load()
or attach() function. Let’s take a look at the syntax of the save()
function.
Syntax:
save(list, file = stop("'file' must be specified"), ascii, version, envir = parent.frame(), compress = isTRUE(!ascii), compression_level, eval.promises, precheck)
Parameters:
- list: The character vector whose values are the names of the objects you want to save.
- file: The connection or the name of the file which saves the data.
- ascii: The default is FALSE. Write the representation of the data or not.
- version: The default is NULL. The version of the workspace which is used for this function.
- envir: The environment which is used to search the saved objects.
- compression_level: The level of the compression.
- precheck: The default is TRUE. Check if the objects exist or not.
After learning what the save()
function does and the syntax of this function, you will learn how to save the objects in the next title below.
How to save objects in R?
If you no longer need to use the objects, you can save the object by the save()
or the saveRDS()
function in R. And in the future, if you need to use these objects, you can reuse the saved objects by the load()
function.
Save the objects with the save()
function
You can save the objects in R by the save()
function.
Look at the example below:
# Create the data frame df <- data.frame( Country = c("Portugal", "Argentina", "France", "Spain", "Croatia"), Golden_Balls = c(7, 7, 5, 4, 1) ) # Save the 'df' data frame to the file named 'num_golden_balls.RData' save(df, file = "num_golden_balls.RData")
After running these statements, the ‘df’ data frame is saved at the file named ‘num_golden_balls.RData’. You can use the ‘df’ data frame in the future by loading the ‘num_golden_balls.RData’ file.
Save a single object in R with the saveRDS()
function
You can save a single object by the saveRDS()
function to prevent being overwritten without any warning.
Look at the example below.
# Create the data frame df <- data.frame( Country = c("Portugal", "Argentina", "France", "Spain", "Croatia"), Golden_Balls = c(7, 7, 5, 4, 1) ) # Save the 'df' data frame to the file named 'num_golden_balls.rds' saveRDS(df, file = "num_golden_balls.rds")
You can also learn the R language with the series of R tutorials here.
Summary
We have shared with you how to save objects in R by the save()
function and the saveRDS()
function. In this way, you can reuse these objects in the future by reading back these objects from the file you saved by the load()
or attach()
function. We hope this tutorial is helpful to you. Thanks!
Maybe you are interested:
- Drop Variable In R: Drop the rows or columns in a data frame
- Load Packages In R: How To Ways
- Clear All In R: Clear The Environment
My name is Thomas Valen. As a software developer, I am well-versed in programming languages. Don’t worry if you’re having trouble with the C, C++, Java, Python, JavaScript, or R programming languages. I’m here to assist you!
Name of the university: PTIT
Major: IT
Programming Languages: C, C++, Java, Python, JavaScript, R