问题
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:
- Use sheet instead of sheetindex
- remove "file=" and just mention x
- 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