-2

I have loaded an Excel.xlsx which has 8 sheets as a list, by

> require (XLConnect)
> wb <- loadWorkbook ("D:/data /Excel.xlsx")    
> list <- readWorksheet (wb, sheet = getSheets (wb)) 

But what I want to get are 8 independent data.frames. So I want to "unlist" the list into data.frames. I've tried ldply, but this combine all the 8 sheets into one data.frame.

Does anyone know how to load sheets under one Excel to be separate data.frames? Or how to convert List elements into separate data.frames? Thanks a lot!

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
Jingyi
  • 1
  • 5
  • Did you check `?list2env`? – David Arenburg Jul 14 '14 at 20:01
  • ah...I don't know. Can you be more specific about how to use list2env? – Jingyi Jul 14 '14 at 20:02
  • 2
    What difference does it make whether they are stored in separate variables or together in a list? The single list is actually _more_ flexible. – joran Jul 14 '14 at 20:04
  • Yes, indeed. But I want to join some sheets into one, and then join this one with some other sheets. How to do this when the sheets are stored in the list? Thanks. – Jingyi Jul 14 '14 at 20:09
  • Depends on what you mean by join, which seems like the sort of detail and clarification that should have been in your question in the first place. `do.call(rbind,list(...))`? Or maybe [this](http://stackoverflow.com/q/8091303/324364)? – joran Jul 14 '14 at 20:14
  • `list2env(list, .GlobalEnv)` will create a data frame for every object you have in `list` (note that the objects have to have names). But you should listen to @joran first (he has a Mac :)) – David Arenburg Jul 14 '14 at 20:14

1 Answers1

0
library(XLConnect)
wb <- loadWorkbook ("D:/data /Excel.xlsx") 
sheets <- getSheets(wb)
invisible(lapply(sheets,function(sheet) 
             assign(sheet,readWorksheet (wb, sheet = sheet ),pos=1)))

This will create data frames for each sheet, named as the sheet names.

However: this business of using assign(...) to create variables silently is generally frowned upon.

jlhoward
  • 58,004
  • 7
  • 97
  • 140