Warning: session_start(): open(/tmp/sess_efe3cb16940297ff61ee76c9d7fc66b9, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Save objects in R: The best ways to save the R objects - LearnShareIT

Save objects in R: The best ways to save the R objects

Save object in r

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:

Posted in R

Leave a Reply

Your email address will not be published. Required fields are marked *