2

Currently, I'm working with Excel workbooks for > 15 sheets. I use the following sequence:

#libraries
library(data.table)
library(openxlsx)    

#load excel file
    wb <- loadWorkbook("Data.xlsx")

    #write sheets into a list of data frames
    name <- names(wb)
    df <- list()
    for (i in 1:length(name)){

      d <- lapply(i, function(i) readWorkbook(wb, sheet = name[i], startRow = 1, colNames = TRUE, rowNames = FALSE, detectDates = TRUE, skipEmptyRows = TRUE, skipEmptyCols = TRUE, rows = NULL, cols = NULL, check.names = FALSE, namedRegion = NULL, na.strings = "NA", fillMergedCells = FALSE))

      df <- append(df, d)
    }



    #assign names to columns
    names(df) <- name

    #merge DFs into one list
    DT <- rbindlist(df)

Now, I've been working mostly with very small data sets, so performance is not an issue. However, I was wondering if there's an easier way to perform this, in case I'm working with big data sets.

Thanks a lot for your input already.

YalDan
  • 328
  • 1
  • 9
  • Why are you using both a `for` loop and `lapply`? – Axeman Oct 10 '17 at 09:40
  • Something like this answer - https://stackoverflow.com/a/11433532/3022126 - could be very easily adapted to load multiple Excel workbooks/sheets – Phil Oct 10 '17 at 09:52
  • Without the loop, it keeps overwriting `d`: `List of 1 $ :'data.frame': 490 obs. of 3 variables: ..$ datetime: num [1:490] 42866 42866 42866 42866 42866 ... ..$ ID : chr [1:490] "t13" "t13" "t13" "t13" ... ..$ value : chr [1:490] "8 mins" "8 mins" "8 mins" "8 mins" ... ` But the list should have 13 entries in this case. Therefore, I added the loop to keep appending the imported values instead of overwriting them. Is this possible merely with lapply? – YalDan Oct 10 '17 at 09:52
  • @Phil that looks like a good one, I'll dig through it, thanks – YalDan Oct 10 '17 at 09:54
  • Yes, something like `l – Axeman Oct 10 '17 at 10:00

0 Answers0