I understand that XLConnect
can be used to read an Excel worksheet into R. For example, this would read the first worksheet in a workbook called test.xls
into R.
library(XLConnect)
readWorksheetFromFile('test.xls', sheet = 1)
I have an Excel Workbook with multiple worksheets.
How can all worksheets in a workbook be imported into a list in R where each element of the list is a data.frame for a given sheet, and where the name of each element corresponds to the name of the worksheet in Excel?
From official
readxl
(tidyverse) documentation (changing first line):Details at: http://readxl.tidyverse.org/articles/articles/readxl-workflows.html#iterate-over-multiple-worksheets-in-a-workbook
excel.link will do the job.
I actually found it easier to use compared to XLConnect (not that either package is that difficult to use). Learning curve for both was about 5 minutes.
As an aside, you can easily find all R packages that mention the word "Excel" by browsing to http://cran.r-project.org/web/packages/available_packages_by_name.html
I tried the above and had issues with the amount of data that my 20MB Excel I needed to convert consisted of; therefore the above did not work for me.
After more research I stumbled upon openxlsx and this one finally did the trick (and fast) Importing a big xlsx file into R?
https://cran.r-project.org/web/packages/openxlsx/openxlsx.pdf
Updated answer using readxl (22nd June 2015)
Since posting this question the
readxl
package has been released. It supports bothxls
andxlsx
format. Importantly, in contrast to other excel import packages, it works on Windows, Mac, and Linux without requiring installation of additional software.So a function for importing all sheets in an Excel workbook would be:
This could be called with:
Old Answer
Building on the answer provided by @mnel, here is a simple function that takes an Excel file as an argument and returns each sheet as a data.frame in a named list.
Thus, it could be called with:
Adding to Paul's answer. The sheets can also be concatenated using something like this:
Libraries needed:
Note that most of XLConnect's functions are already vectorized. This means that you can read in all worksheets with one function call without having to do explicit vectorization:
With XLConnect 0.2-0 lst will already be a named list.