David Luong
David Luong

Reputation: 443

How to import multiple xlsx sheets in R

I am learning R studio right now and I was just wondering how do I import an xlsx file with multiple sheets (8 sheets).

So far.. I have:

library(readxl)
(filename) <- read_excel("(file location).xlsx")
view(filename)

It prints out only the first sheet of the excel file, but I would appreciate it if it printed out all of them.

Also, is it better to use CSV files or xlsx?

thanks, guys.

Upvotes: 2

Views: 9300

Answers (2)

Wimpel
Wimpel

Reputation: 27792

This will work for all sheets in the workbook

lapply(excel_sheets(path), read_excel, path = path)

read ?excel_sheets

Upvotes: 3

Maurits Evers
Maurits Evers

Reputation: 50738

You could do

lst <- lapply(1:8, function(i) read_excel("file_name.xlsx", sheet = i))

This will store all 8 sheets in a list of data.frames.

Upvotes: 11

Related Questions