0

I want to import all the worksheets from an Excel file (except the first one) into R. Right now all the dataframes are sitting inside a list and I want them out as individual dataframes, but don't know how to do that. A for loop maybe?

# read rawdatabase in Excel file and create list with a dataframe for each sheet
library(magrittr)
library(dplyr)
library(readxl)
workbook <- "rawdata.xlsx"

# Because I don't want the first sheet
noReadme <- "README" 
sheets <- excel_sheets(workbook) %>% setdiff(noReadme)

# Create list with all the remaining dataframes
l <- lapply(sheets, function(v) read_excel(workbook, sheet = v))

# Give each dataframe the proper name
names(l) <- sheets 

I want this to be as dynamic as possible, so that I re-use this script with other excel files with more or less sheets.

mariachi
  • 215
  • 1
  • 7
  • won't this further encapsulate my data? then instead of inside a list, the dfs are inside a new environment – mariachi Feb 05 '16 at 15:58
  • The function can be used to put all of the `data.frame`s in your `list` into your global environment, which is what it sounds like you're asking for. Try `list2env(l, envir = GlobalEnv)`. – A5C1D2H2I1M1N2O1R2T1 Feb 05 '16 at 16:01
  • Yes! that is what I want! I wasn't understanding that you could use and existing environment. list2env(l, envir = .GlobalEnv) could you submit it as an answer? – mariachi Feb 05 '16 at 16:05

0 Answers0