-3

I'm new in the R world, so I have this question:

  • I have a principal folder. In that folder, I have several subfolders, and then, into this subfolders, anothers subfolders. For example:

Works/Folder A,B,C.../Economic/New/File.xlsx

This structure repeats for Folder A, B, C, D, and the rest. What I need is import every "file.xlsx" and merge all dataframes into one.

Thank you very much!

  • 1
    Have you tried anything? SO has several similar questions that if you search something like [`[r] read multiple files`](https://stackoverflow.com/search?q=%5Br%5D+read%20multiple%20files), you'll find several really good matches. I suspect that https://stackoverflow.com/q/5319839 will suffice as a duplicate. – r2evans May 06 '20 at 03:09
  • @r2evans That I guess is to create separate dataframes. I think OP needs one dataframe in the end. There might be a better target but I think this would work https://stackoverflow.com/questions/43900965/read-several-files-in-different-directories-in-r – Ronak Shah May 06 '20 at 03:16
  • 1
    You're right, they'll need `do.call(rbind, ...)` or `dplyr::bind_rows` or `data.table::rbindlist` after that. – r2evans May 06 '20 at 03:19

1 Answers1

1

An approach using map_df, list.files. Assuming that your files all hold their data on the first worksheet.

library(tidyverse)
myConcat <- 
  list.files("Economic/New", recursive = TRUE, pattern = 
            "(?i)file.xlsx", full.names=TRUE) %>% 
  map_df( ~ readxl::read_excel(.x, sheet = 1))

# Runs and works on my own files, (with different directories and names, of course.)

The (?i) makes it case insensitive; I noticed you had both File.xlsx and file.xlsx.

David T
  • 1,993
  • 10
  • 18