load equally named excel files located in subfolders into R and bind them

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?

Upvotes: 0

Views: 154

Answers (1)

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)

Upvotes: 1

Related Questions