简体   繁体   中英

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.

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: 在此处输入图像描述

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:

在此处输入图像描述

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM