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.
# 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,
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")
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!