install.packages("readxl")
library("readxl")
data <- read_excel("spreadsheet.xlsx")
sheets <- excel_sheets("spreadsheet.xlsx")
sheetList <- as.list(sheets) #convert sheets to list
for (s in sheetList){
read_excel("spreadsheet.xlsx",sheet=s)
} #want to do such that when looping through each sheet, it would read in the data and be assigned its own 'variable'(dataframe)
Asked
Active
Viewed 1,272 times
0
-
Do you want to read all the worksheets in one workbook or do you want to read all the sheets located in different workbooks? – Henry Cyranka Nov 24 '18 at 20:19
-
all worksheet in one workbook – E. Huang Nov 24 '18 at 20:37
-
Possible duplicate of [Read all worksheets in an Excel workbook into an R list with data.frames](https://stackoverflow.com/questions/12945687/read-all-worksheets-in-an-excel-workbook-into-an-r-list-with-data-frames) – phiver Nov 25 '18 at 13:06
1 Answers
1
I wouldn't use a for loop. Easier to loop through the lists with lapply. Here is how I would do it.
library(readxl)
sheets_to_read <- excel_sheets("spreadsheet.xlsx") ##Obtain the sheets in the workbook
list_with_sheets <- lapply(sheets_to_read,
function(i)read_excel("spreadsheet.xlsx", sheet = i))
names(list_with_sheets) <- sheets_to_read ##Add names
list2env(mylist ,.GlobalEnv) ##This function should put them all in the global environment

Henry Cyranka
- 2,970
- 1
- 16
- 21
-
so that looks right, but wondering, is it possible to 'save' each of the sheet as a new dataframe so i can access them from a tab in R? – E. Huang Nov 24 '18 at 20:43
-
-