将位于子文件夹中的Excel文件同样命名为R中并绑定

发布于 2025-02-03 08:08:24 字数 567 浏览 3 评论 0原文

我正在寻找一种解决方案,将几个位于几个子文件

夹中的excel文件加载到R中。 :

setwd('C:\Users\RandomGuy\Workspace\')
dir()

combination <- dir()[grepl('MZB', dir())]
# loop
for (i1 in 1:length(combination)) print(readxl::excel_sheets(combination[[i1]])); rm(i1)
# checking of 
comb <- list()
for (i1 in 1:length(combination)) {
  comb[[i1]] <- readxl::read_xlsx(combination[[i1]], sheet='')
  print(names(comb[[i1]])) }; rm(i1)
comb <- do.call('rbind', comb)

现在,我有一个问题,那些Excel文件不是位于同一文件夹中的,而是在几个子文件夹中。 是否有方法可以访问这些方法并将同样命名为Excel文件(或一次至少一个表)合并到R中?

I am searching for a solution to load several equally named Excel files located in several subfolders into R.

When those excel files were located in the same folder and all contain "MZB" as part of their name, I could easily apply a loop such as:

setwd('C:\Users\RandomGuy\Workspace\')
dir()

combination <- dir()[grepl('MZB', dir())]
# loop
for (i1 in 1:length(combination)) print(readxl::excel_sheets(combination[[i1]])); rm(i1)
# checking of 
comb <- list()
for (i1 in 1:length(combination)) {
  comb[[i1]] <- readxl::read_xlsx(combination[[i1]], sheet='')
  print(names(comb[[i1]])) }; rm(i1)
comb <- do.call('rbind', comb)

Now, I have the issue, that those excel files are not located in the same folder but instead in several subfolders.
Is there are way to access those and merge equally named Excel files (or at least one sheet at a time) into R?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

方觉久 2025-02-10 08:08:24
files <- list.files(pattern = "listen.xlsx", path = "C:/...", recursive = TRUE, full.names = TRUE)
lst_of_frames <- lapply(files, readxl::read_excel, sheet='')


oneframe <- do.call("rbind.data.frame", lst_of_frames)
files <- list.files(pattern = "listen.xlsx", path = "C:/...", recursive = TRUE, full.names = TRUE)
lst_of_frames <- lapply(files, readxl::read_excel, sheet='')


oneframe <- do.call("rbind.data.frame", lst_of_frames)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文