title: Importing multiple Excel files with filenames in R
body: <p>I have about one hundred excel files which I need to import to R and merge. All excel-files have four columns and each one needs to be imported. The files look like one below:</p>
  
  <pre><code>1     127          122
  1     87      
  2     107     
  1     136    k    
  1     210     
  </code></pre>
  
  <p>I also need to add filename as fifth column for each row. All excel files are in the same folder.</p>
  
  <p>So far I have tried following:</p>
  
  <pre><code>library(xlsx)
  setwd("c:/temp/")
  filenames &lt;- list.files(pattern=".xls")
  do.call("rbind", lapply(filenames, function(x) read.xlsx(file=x, sheetIndex=1, colIndex=(1:4), header=FALSE, FILENAMEVAR=x)))
  </code></pre>
  
  <p>I get following error: Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match</p>
  
  <p>I have located the problem to empty cells in third and fourth columns as function works perfectly if I limit it only to first and second columns.</p>