R Series

How to write objects into a file in R?

You can save an R object like a data frame as either an RData file or an RDS file. RData files can store multiple R objects at once, but RDS files are the better choice because they foster reproducible code.

We can also save objects likes data frame into a csv files and can be used later.

The ‘write.csv( )’ command can be used to save an R data frame as a .csv file. While variables created in R can be used with existing variables in analyses, the new variables are not automatically associated with a data frame. For example, suppose we read in a .csv file under the data frame and done some changes to it.

Example:

# Creating a data frame
> df <- data.frame(
+ c1 = c(1:10),
+ c2 = c(101:110)
+ )
> df
   c1  c2
1   1 101
2   2 102
3   3 103
4   4 104
5   5 105
6   6 106
7   7 107
8   8 108
9   9 109
10 10 110

# Saving that data frame into a csv file
> write.csv(df , 'hello.csv')

To save data as an RData object, use the save function. To save data as a RDS object, use the saveRDS function. In each case, the first argument should be the name of the R object you wish to save. You should then include a file argument that has the file name or file path you want to save the data set to.

For example, if you have three R objects, ab, and c, you could save them all in the same RData file and then reload them in another R session:

> a <- c(1:10)
> b <- c('hello', 'hey', 'hi')
> c <- rnorm(10)
> save(a, b, c, file = "stuff.RData")
> load("stuff.RData")

To save all the data of your workspace we need ls() function. This function return a vector of character strings giving the names of the objects in the specified environment.

> save(list = ls(), file = "obj.RData")
> load("obj.RData")

Conclusion

Hence, we saw how to save and write objects like data frames into different files. with an example each, also
how to store the environmental variables in an RData file.

This brings the end of this Blog. We really appreciate your time.

Hope you liked it.

Do visit our page www.zigya.com/blog for more informative blogs on Data Science

Keep Reading! Cheers!

Zigya Academy
BEING RELEVANT

Zigya Acadmey

Share
Published by
Zigya Acadmey

Recent Posts

Understanding Standard Form of Numbers: An Explanation With Examples

Through the standard form offers different advantages in mathematical calculations and scientific notation. Firstly, it…

5 months ago

How to deal with stress and anxiety in college

Introduction Stress is a feeling caused by an external trigger that makes us frustrated, such…

6 months ago

Why is Sociology Important These Days?

Sociology is a broad discipline that examines societal issues. It looks at the meaningful patterns…

6 months ago

How to Convert Inches to mm

Some info about Inch Inches are a unique measure that persuades us that even the…

8 months ago

Antilogarithms – Definition, Methods, and Examples

You should be familiar with logarithms to understand antilogarithms in a better manner. Logarithms involve…

10 months ago

नाटककार सुरेंद्र वर्मा

यहां "नाटककार सुरेंद्र वर्मा" पुस्तक की पीडीएफ विद्यार्थी, शोधार्थी और जो इसका अभ्यास के लिए…

10 months ago