简体   繁体   English

将多个文件保存为.Rdata

[英]Saving multiple files as .Rdata

I was wondering if there is a way of using the save() option to save multiple files of data.我想知道是否有一种方法可以使用 save() 选项来保存多个数据文件。 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.我想以 .Rdata 的形式保存所有这些文件,但不确定如何在不多次使用 save() 的情况下解决这个问题。 I am new to R.我是 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:在这里,我使用了 R 中的“数据集”package 中包含的数据集。首先检查您的目录以查看其保存位置:

getwd()

Then see where that is from the output:然后查看来自 output 的位置:

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

Creating basic rdata file创建基本的 rdata 文件

Then go ahead and run the code:然后 go 并运行代码:

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.如果您的意思是保存多个不同类型的对象,那就有点问题了,因为 csv 或 spss 文件之类的东西不容易强制转换。 One option is to include the mapply function. I've also used R datasets here as an example:一种选择是包括mapply function。我在这里还使用了 R 数据集作为示例:

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:您现在可以在目录中看到:

在此处输入图像描述

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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