Search code examples
rxlsxreadxl

R import some xlsx files starting with same string, ignore the other in the same folder


I would like to import some xlsx files into rstudio. These files are all in the same folder, but there are also some other xlsx files in there, that I am not interested in:

  • Alles_2017.xlsx
  • Alles_2018.xlsx
  • Alles_2019.xlsx
  • Alles_2020_incl_Oct.xlsx
  • Blabla.xlsx
  • Idontknow.xlsx

I would like to import all four "Alles_...." files building a single dataframe (called for example All) containing all this data. And I would like the function to ignore every other file not starting with "Alles_"

Which function can I use?

Thank you in advance!


Solution

  • Firstly, I would set the working directory with setwd(), and then:

    dir()[grepl('Alles', ignore.case = T, dir())] -> files    
    do.call('rbind', lapply(files, function(file) openxlsx::read.xlsx(file)))
    

    In case you want the second sheet:

    do.call('rbind', lapply(files, function(file) openxlsx::read.xlsx(file, sheet = 2)))