How to read multiple .xls files in r?

一世执手 提交于 2020-08-12 00:09:42

问题


I have 18 files(.xls) in list and I want to read them in one go

Here is my codes below:

filenames=list.files("C:/Users/ozgur.alptekin/Downloads/elif")

df.list=lapply(filenames, function(x) read_excel(file = x,sheetIndex = 1,as.data.frame = TRUE,header = TRUE))

it did not work

Could you please tell me what I have done wrong and how I should do it?


回答1:


Can you simply try a loop?

filenames=list.files("C:/Users/ozgur.alptekin/Downloads/elif")

for (i in 1:length(filenames) {
assign(paste0("file_", i), 
read_excel(file = filenames[i],sheetIndex = 1, as.data.frame = TRUE, header = TRUE), envir = .GlobalEnv)
}

reply if it works.




回答2:


You can use the same code with some changes. Under read_excel:

  1. Use sheet instead of sheetindex
  2. remove "file=" and just mention x
  3. Don't think as.data.frame works in read_excel

Then,

filenames=list.files("C:/Users/ozgur.alptekin/Downloads/elif")    
df.list=lapply(filenames, function(x) read_excel(x,sheet = 1,header = TRUE))

Then to convert it into a data.frame format, use the below

Appending of all the different data into one master data

master_file = as.data.frame(do.call(rbind,df.list))


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

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