R:将工作区中的所有 data.frames 保存到单独的 .RData 文件中 [英] R: Save all data.frames in workspace to separate .RData files
问题描述
我有几个 data.frames
在我想保存到单独的 .RData 文件的环境中.有没有可以保存到整个工作区的功能?
I have several data.frames
in an environment which I would like to save into separate .RData files. Is there a function which is able to save to whole workspace?
我通常只使用以下函数:
I usually just do this with the following function:
save(x, file = "xy.RData")
但是有没有一种方法可以一次单独保存所有 data.frames?
but is there a way I could save all the data.frames separately at once?
推荐答案
创建一堆不同的文件并不是 save()
的矢量化方式.在这里使用循环可能更好.首先,获取所有 data.frame 名称的向量.
Creating a bunch of different files isn't how save()
is vectorized. Probably better to use a loop here. First, get a vector of all of your data.frame names.
dfs<-Filter(function(x) is.data.frame(get(x)) , ls())
现在将每个写入一个文件.
Now write each to a file.
for(d in dfs) {
save(list=d, file=paste0(d, ".RData"))
}
或者,如果您只想将它们全部放在一个文件中
Or if you just wanted them all in one file
save(list=dfs, file="alldfs.RData")
这篇关于R:将工作区中的所有 data.frames 保存到单独的 .RData 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!