0

In followup to this post: Import multiple excel sheets using openxlsx , I would like to import multiple sheets from multiple xlsx files into one R data frame using openxlsx (or any other package that isn't sported by Java due to memory problems).

At the moment my code looks like this:

require(openxlsx)   
file.list <- list.files(recursive=T,pattern='*.xlsx')

for (i i 1:length(file.list)){                                           
   wb <- loadWorkbook(file.list[i])           #select a file & load workbook
   sheet <-sheets(wb)                #get sheet list

     for (j in 1:length(sheet)){ 
       tmp<-read.xlsx(file.list[i], sheet=j, cols= c(1:4,6,7,9:12,14,15,17:30),
                      startRow=5, skipEmptyRows = T, colNames=F)
        tmp$file = i
         if (i==1&j==1) dataset<-tmp else dataset<-rbind(dataset,tmp)     
          }
        }

However the rbind function returns the following error

  Error in rbind(deparse.level, ...) : 
  numbers of columns of arguments do not match

I think that my mistake lays somewhere in the read.xlsx code but I can't figure out where I'm going wrong.

Any help or suggestions are very welcome!

Community
  • 1
  • 1
Elisah
  • 115
  • 1
  • 1
  • 6
  • Read the files into a list. Then you will be able to inspect each of them individually. See gregor's answer on [this post](http://stackoverflow.com/questions/17499013/how-do-i-make-a-list-of-data-frames) for tips on doing this. – lmo Jul 07 '16 at 13:23
  • Thanks, his tips on creating a list of files is very helpful for looking at the input files. But because my files are different at start (that's why I only read in specific columns) this does not show me the problem with the rbind code. Would there be something missing from my read.xlsx perhaps? – Elisah Jul 07 '16 at 14:11
  • You can subset the files as you read them in in the same manner. For example, you can use `lapply(j in 1:length(sheet), function(j) read.xlsx, cols= c(1:4,6,7,9:12,14,15,17:30), startRow=5, skipEmptyRows = T, colNames=F))` to read in sheets from a workbook in your inner loop. – lmo Jul 07 '16 at 14:16
  • Thanks! How would you then connect this to the inner loop? – Elisah Jul 08 '16 at 07:52
  • Try this out on a single workbook. When you get it running, then it can replace the inner loop. – lmo Jul 08 '16 at 11:38

0 Answers0