How to read in multiple “.xlsx” files to R

自古美人都是妖i 提交于 2020-01-19 18:07:17

问题


Having trouble trying to read in multiple .xlsx files to R from the same directory. I keep getting the following error.

"Error in path.expand(file) : argument "file" is missing, with no default"

My code is as follows.

require(.xlsx)
Files=list.files(path="I:/Marcs_Discretinization_try_1/Attempt1/Actual     Data", pattern=".xlsx")
sapply(Files, read.xlsx2(sheetIndex=8))

The output of object Files looks like this which seemingly does not have the attached path.

 [1] "2015-B1-2OR.xlsx"    "2015-B1-OR10-B.xlsx" "2015-B1-OR10.xlsx"   "2015-B1-OR19.xlsx"   "2015-B2-OR19.xlsx"  
 [6] "2015-O1-2OR.xlsx"    "2015-O1-OR10-B.xlsx" "2015-O1-OR10.xlsx"   "2015-O2-2OR.xlsx"    "2015-O2-OR10-B.xlsx"
[11] "2015-O2-OR10.xlsx"   "2015-X1-2OR.xlsx"    "2015-X1-OR10-B.xlsx" "2015-X1-OR10.xlsx"   "2015-X2-2OR.xlsx"   
[16] "2015-X2-OR10-B.xlsx" "2015-X2-OR10.xlsx"  

回答1:


You want to merge all Excel files in a folder?

library(xlsx)
setwd("C:/Users/rshuell001/Desktop/excel_files")
data.files = list.files(pattern = "*.xlsx")
data <- lapply(data.files, function(x) read.xlsx(x, sheetIndex = 1))

for (i in data.files) {
    data <- rbind(data, read.xlsx(i, sheetIndex = 1))
}


来源:https://stackoverflow.com/questions/33771402/how-to-read-in-multiple-xlsx-files-to-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!