将包含同名对象的多个 .RData 文件合并为一个 .RData 文件 [英] Combine multiple .RData files containing objects with the same name into one single .RData file

查看:56
本文介绍了将包含同名对象的多个 .RData 文件合并为一个 .RData 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多 .RData 文件,其中包含我在之前的分析中保存的一个数据框,并且每个加载的文件的数据框都具有相同的名称.因此,例如使用 load(file1.RData) 我得到一个名为 'df' 的数据框,然后使用 load(file2.RData) 我得到一个具有相同名称 'df' 的数据框.我想知道是否有可能将所有这些 .RData 文件组合成一个大的 .RData 文件,因为我需要一次加载它们,每个 df 的名称都等于文件名,这样我就可以使用不同的数据帧.

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. 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'. 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.

我可以使用下面的代码来做到这一点,但它非常复杂,必须有更简单的方法来做到这一点……谢谢您的建议.

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.

假设我有 3 个 .RData 文件,并希望将所有文件保存在一个名为main.RData"的文件中,并使用它们的特定名称(现在它们都以df"的形式出现):

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'):

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")
}

推荐答案

这里有一个包含多个现有帖子的选项

Here's an option that incorporates several existing posts

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

将多个数据帧读入单个命名列表(如何将对象加载到我从 R 数据文件中指定的变量名中?)

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?)

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).

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

这篇关于将包含同名对象的多个 .RData 文件合并为一个 .RData 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆