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?
我在一个环境中有几个data.frame,我想将它们保存到单独的. rdata文件中。是否有一个函数可以保存到整个工作区?
I usually just do this with the following function:
我通常用以下函数来做:
save(x, file = "xy.RData")
but is there a way I could save all the data.frames separately at once?
但是有没有一种方法可以同时保存所有的数据。frame ?
3 个解决方案
#1
8
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.
创建一堆不同的文件并不是save()的矢量方式。最好在这里使用循环。首先,获取所有数据的向量。
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")
#2
1
To save your workspace you just need to do:
要保存你的工作空间,你只需要:
save.image("willcontainworkspace.RData")
This creates a single file that contains the entire workspace which may or may not be what you want but your question wasn't completely clear to me.
这样就创建了一个包含整个工作区的文件,这个工作区可能是您想要的,也可能不是您想要的,但是您的问题我不是很清楚。
#3
0
Similar to @MrFlick's approach, you can do something like this:
类似于@MrFlick的方法,您可以做如下事情:
invisible({
sapply(ls(envir = .GlobalEnv), function(x) {
obj <- get(x, envir = .GlobalEnv)
if (class(obj) == "data.frame") {
save(obj, file = paste0(x, ".RData"))
}
})
})
#1
8
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.
创建一堆不同的文件并不是save()的矢量方式。最好在这里使用循环。首先,获取所有数据的向量。
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")
#2
1
To save your workspace you just need to do:
要保存你的工作空间,你只需要:
save.image("willcontainworkspace.RData")
This creates a single file that contains the entire workspace which may or may not be what you want but your question wasn't completely clear to me.
这样就创建了一个包含整个工作区的文件,这个工作区可能是您想要的,也可能不是您想要的,但是您的问题我不是很清楚。
#3
0
Similar to @MrFlick's approach, you can do something like this:
类似于@MrFlick的方法,您可以做如下事情:
invisible({
sapply(ls(envir = .GlobalEnv), function(x) {
obj <- get(x, envir = .GlobalEnv)
if (class(obj) == "data.frame") {
save(obj, file = paste0(x, ".RData"))
}
})
})