Razvan Grecu
Razvan Grecu

Reputation: 1

Reading the second excel tabs from multiple Excel workbooks in R

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.

Upvotes: 0

Views: 1561

Answers (1)

Mike Stanley
Mike Stanley

Reputation: 1490

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")

Upvotes: 2

Related Questions