0

I have troubles with reading data stored in the 2nd tab of multiple Excel spreadsheets which are stored locally. I succeeded to read all the data from the first tab of these spreadsheets using the syntax:

library(readxl)
filenames2017 <-list.files(pattern = "*.xls")
final2017.df <- do.call("rbind", lapply(filenames2017, read_excel))  

However, I could not find any solution for importing data from other specific tabs than the first tab.

Uwe
  • 41,420
  • 11
  • 90
  • 134

1 Answers1

2

read_excel has a sheet argument where you can specify the name or number of the sheet:

read_excel("example.xlsx", sheet = 2)
read_excel("example.xlsx", sheet = "some_sheet")

So you can use this to read the second sheet. readxl::excel_sheets will return a list of sheets if you don't know in advance how many there are.

You can pass the sheet argument into read_excel inside your lapply by adding it as another argument, like:

lapply(filenames2017, read_excel, sheet = "the_sheet")
Mike Stanley
  • 1,420
  • 11
  • 13
  • Thank you for the reply, but this does not solve my problem. I know how to read a specific tab of an Excel workbook, but I did not manage to import data from a specific tab (other than the first) of multiple workbooks. – Razvan Grecu Sep 23 '17 at 16:40
  • `lapply` is in this format: `lapply(.x, .f, ...)` - `.x` is the list and `.f` is the function, you know that. `...` is extra arguments to `.f`. So you can put `sheet = 'thesheet'` as the third argument to `lapply` and it will be passed on to your function. I will clarify that in my answer, thanks. – Mike Stanley Sep 26 '17 at 18:14