Adrian
Adrian

Reputation: 21

Saving multiple files as .Rdata

I was wondering if there is a way of using the save() option to save multiple files of data. I wanted to save all these files in the form of .Rdata, but wasn't sure how to approach this without using save() multiple times. I am new to R.

Upvotes: 2

Views: 2225

Answers (1)

Shawn Hemelstrand
Shawn Hemelstrand

Reputation: 3228

Checking directory

As someone already mentioned above, you can save something as multiple objects and run it that way. Here I used the datasets included in the 'datasets' package within R. First check your directory to see where its getting saved:

getwd()

Then see where that is from the output:

[1] "C:/Users/DELL/Dropbox/My PC (DESKTOP-SUOCLVS)/Desktop/Research Tools/R Directory"

Creating basic rdata file

Then go ahead and run the code:

df1 <- iris
df2 <- mtcars
save(df1, df2, 
     file = "mydata.rdata")

You'll see now its saved in the directory: enter image description here

Other file types

If you mean saving multiple objects of different types, that is a bit more of an issue, as something like a csv or spss file isn't easy to coerce. One option is to include the mapply function. I've also used R datasets here as an example:

library(tidyverse)

myList <- list(diamonds = diamonds, 
               cars = cars)
mapply(write.csv, myList, file=paste0(names(myList), '.csv'))

Which you can now see in the directory:

enter image description here

enter image description here

Upvotes: 1

Related Questions