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:
1 127 122
1 87
2 107
1 136 k
1 210
I also need to add filename as fifth column for each row. All excel files are in the same folder.
So far I have tried following:
library(xlsx)
setwd("c:/temp/")
filenames <- list.files(pattern=".xls")
do.call("rbind", lapply(filenames, function(x) read.xlsx(file=x, sheetIndex=1, colIndex=(1:4), header=FALSE, FILENAMEVAR=x)))
I get following error: Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match
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.