Importing multiple Excel files with filenames in R

2020-02-29 23:18发布

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.

标签: r excel import
1条回答
beautiful°
2楼-- · 2020-03-01 00:21

Figured it out myself. The key was to use rbind.fill instead of rbind.

library(plyr)
df.list <- lapply(filenames, function(x) read.xlsx(file=x, sheetIndex=1,
                  colIndex=1:4,as.data.frame=TRUE, header=FALSE, FILENAMEVAR=x))
final.df <- rbind.fill(df.list)
查看更多
登录 后发表回答