简体   繁体   English

将包含具有相同名称的对象的多个.RData文件合并到一个.RData文件中

[英]Combine multiple .RData files containing objects with the same name into one single .RData file

I have many many .RData files containing one dataframe that I had saved in a previous analysis and the data frame has the same name for each file loaded. 我有很多.RData文件包含我在之前的分析中保存过的一个数据框,并且数据框对于每个加载的文件都具有相同的名称。 So for example using load(file1.RData) I get a data frame called 'df', then using load(file2.RData) I get a data frame with the same name 'df'. 所以例如使用load(file1.RData)我得到一个名为'df'的数据框,然后使用load(file2.RData)我得到一个名为'df'的数据框。 I was wondering if it is at all possible to combine all these .RData files into one big .RData file since I need to load them all at once, with the name of each df equal to the file name so I can then use the different data frames. 我想知道是否可以将所有这些.RData文件合并到一个大的.RData文件中,因为我需要一次加载它们,每个df的名称等于文件名,这样我就可以使用不同的数据帧。

I can do this using the code below, but it is very intricate, there must be a simpler way to do this… Thank you for your suggestions. 我可以使用下面的代码执行此操作,但它非常复杂,必须有一个更简单的方法来执行此操作...感谢您的建议。

Say I have 3 .RData files and want to save all in a file called "main.RData" with their specific name (now they all come out as 'df'): 假设我有3个.RData文件,并希望将所有文件保存在名为“main.RData”的文件中,并使用其特定名称(现在它们都以“df”形式出现):

all.files = c("/Users/fra/file1.RData", "/Users/fra/file2.RData", "/Users/fra/file3.RData")
assign(gsub("/Users/fra/", "", all.files[1]), local(get(load(all.files[1]))))
rm(list= ls()[!(ls() %in% (ls(pattern = "file")))])
save.image(file="main.RData")


all.files = all.files = c("/Users/fra/file1.RData", "/Users/fra/file2.RData", "/Users/fra/file3.RData")

for (f in all.files[-1]) {
  assign(gsub("/Users/fra/", "", f), local(get(load(f))))
  rm(list= ls()[!(ls() %in% (ls(pattern = "file")))])
  save.image(file="main.RData")
}

I think the best answer I saw was the code below, which I copied from an SO answer which I can't track down right now. 我认为我看到的最佳答案是下面的代码,我从SO答案中复制了这些答案,我现在无法追查。 Apologies to the original author. 向原作者道歉。

resave <- function(..., list = character(), file) {
   previous  <- load(file)
   var.names <- c(list, as.character(substitute(list(...)))[-1L])
   for (var in var.names) assign(var, get(var, envir = parent.frame()))
   save(list = unique(c(previous, var.names)), file = file)
}
#I took advantage of the fact the load function 
#returns the name of the loaded variables, so 
#I could use the function's environment instead of creating one.
#And when using get, I was careful to only look in the 
#environment from which the function is called, i.e. parent.frame()

Here's an option that incorporates several existing posts 这是一个包含几个现有帖子的选项

all.files = c("file1.RData", "file2.RData", "file3.RData")

Read multiple dataframes into a single named list ( How can I load an object into a variable name that I specify from an R data file? ) 将多个数据帧读入单个命名列表( 如何将对象加载到我从R数据文件指定的变量名中?

mylist<- lapply(all.files, function(x) {
  load(file = x)
  get(ls()[ls()!= "filename"])
})

names(mylist) <- all.files #Note, the names here don't have to match the filenames

You can save the list, or transfer the dataframes into the global environment prior to saving ( Unlist a list of dataframes ) 您可以保存列表,或在保存之前将数据帧传输到全局环境中( 取消列出数据帧列表

list2env(mylist ,.GlobalEnv)

Alternatively, if the dataframes were identical and you wanted to create a single big dataframe, you could collapse the list and add a variable with names of contributing files ( Dataframes in a list; adding a new variable with name of dataframe ). 或者,如果数据帧相同并且您想要创建单个大数据帧,则可以折叠列表并添加具有贡献文件名称的变量( 列表中的数据帧;添加名称为dataframe的新变量 )。

all <- do.call("rbind", mylist)
all$id <- rep(all.files, sapply(mylist, nrow))

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

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