Open In App

How to export dataframe to RDATA file in R?

Last Updated : 25 Nov, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In this, article we are going to save the information of a data frame in an RDATA file and display the information of the file using R Programming language. To save the information of a data frame in a file and display the information of the file in R language is as follows:

  1. Using the save function to save the file .RData format.
  2. Using the load() function to load that saved.RData file
  3. Using the file.info() function to get the information of a particular file.

Step 1: Use the save() function to save the file.RData format

In this step user needs to call the save(), function with the name of the new file and with its format passed as its parameter, This function will simply save the file as per the user-specified parameter in the working directory.

save() function: This function writes an external representation of R objects to the specified file.

Syntax: save(…, list = character(), file = stop(“‘file’ must be specified”),ascii = FALSE, version = NULL, envir = parent.frame(), compress = isTRUE(!ascii), compression_level, eval.promises = TRUE, precheck = TRUE)

In this example, we will be simply saving a data frame in new.RData file in the working directory.

R




gfg_data = data.frame(A = c(7,6,2,8,1),
                     B = c(4,2,9,7,3),
                     C = c(1,7,2,6,8))
 
print("Dataframe:->")  
print(gfg_data)
save(gfg_data, file = "gfg.RData")


Output:

[1] "Dataframe:->"
  A B C
1 7 4 1
2 6 2 7
3 2 9 2
4 8 7 6
5 1 3 8

Step 2: Use the load() function to load that saved.RData file

In this step user just need to call the load() function with the name of the file saved in step-1 as its parameter, further, this will help to load the save file in the console so that the user can manage the operation on this file.

Load() function: This function is used to reload datasets written with the function save.

Syntax: load(file, envir = parent.frame(), verbose = FALSE)

Parameters:

  • File:-a character string giving the name of the file to load.
  • envir:-the environment where the data should be loaded.
  • Verbose:-should item name be printed during loading?needs

Under this example, we will be loading the.RData file using the load() function which was saved in the previous example.

R




load("gfg.RData")


Output:

  A B C
1 7 4 1
2 6 2 7
3 2 9 2
4 8 7 6
5 1 3 8

Step3: Using file.info() function to get the information of a particular file.

This is the final step to save the information of a data frame in a file and displays its information, Here the step1 and step2 is used to save the information of the given data frame in a particular file and this step3 will be used to displaying the information of the file where the given data-frame is been saved and for this, we will be using the file.info() function with the name of the previously loaded file in the console to display the information of the file saved.

file.info() function: This is a utility function used to extract information about files on the user’s file systems.

Syntax: file.info(…, extra_cols = TRUE)

Parameter:

  • …:-character vectors containing file paths
  • extra_cols:-Logical: return all cols rather than just the first six.

Returns: size , isdir, mode, ctime, time, atime and exe

Using the file.info() function in R language we will be displaying the completed information of the file saved and loaded in the previous examples.

R




#load("gfg.RData")
file.info("gfg.RData")


Output:

          size isdir mode               mtime               ctime
gfg.RData  185 FALSE  666 2023-07-28 15:35:33 2023-07-28 15:29:53
                        atime exe
gfg.RData 2023-07-28 15:35:34  no


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads