Ozgur Alptekın
Ozgur Alptekın

Reputation: 505

How to read multiple .xls files in r?

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?

Upvotes: 1

Views: 4021

Answers (2)

Sujit Mendon
Sujit Mendon

Reputation: 41

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))

Upvotes: 4

Varun Rajan
Varun Rajan

Reputation: 276

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.

Upvotes: 2

Related Questions