1
votes

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.

2
IMHO: csv usually works better and faster, but if your data is already stored (and formatted!) in excel,please leave it there.Wimpel
yes my data is stored/formatted in excel already. I noticed csv only saves one particular sheet. can save more than one at once?David Luong
yes, use/write a simple vba macroWimpel
@Wimpel ty!! just figured it out yesterday. last question, my data is formatted but then it reads weirdly in R, like the data has lots of spaces in it. is there anyway to fix this? lmk if this is unclear, im not the best at explainingDavid Luong

2 Answers

9
votes

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.

3
votes

This will work for all sheets in the workbook

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

read ?excel_sheets