4

There are many posts about XLConnect and reading excel files in R with XLConnect like How to read multiple excel sheets in R programming?, including rbind function, but no one answers this question:

If i had multiple excel .xls files in a directory how can i use a loop for reading and merging each one in order?

I have a directory so i do this:

 setwd("C:/Users/usuario/Desktop")
  library(rjava)
  library(XLConnect)

That directory has 28 excel files named like this:

 Bitacora_Metrocali_01_02_2014C
 Bitacora_Metrocali_02_02_2014C
         .    ...    ...
 Bitacora_Metrocali_28_02_2014C

So i need to merge them using the function: Merge(x,y,all=T)

So it can add new columns to the dataframe. The thig is, i need a dataframe that starts merging the first wht the second,and then adding all the new sheets in order. All excel files of interest are in sheet 1.

THX!

1
  • I can read them, and merge too, i mean, one per one. But i have too many files. In the example i have 28, but there are more...for real. I need a loop to do that. Thx
    – JULIAN
    Mar 14, 2014 at 2:48

2 Answers 2

3

Does this work for you:

# This will give you a vector of the names of files in your current directory 
# (where I've assumed the directory contains only the files you want to read)
data.files = list.files()

# Read the first file
df = readWorksheetFromFile(file=data.files[1], sheet=1)

# Loop through the remaining files and merge them to the existing data frame
for (file in data.files[-1]) {
    newFile = readWorksheetFromFile(file=file, sheet=1)
    df = merge(df, newFile, all=TRUE)
}
1
  • It gets some as.posixct errors but that is cuz the excel file is so complex. Thx! I did with others and works.
    – JULIAN
    Mar 14, 2014 at 21:07
2

Here's an lapply and Reduce approach I am using the the read.xls from gdata package, as you mentioned xls files. If it is xlsx instead, substitute read.xls with readWorksheetFromFile and load the appropriate libraries.

library(gdata)
data.files = list.files(pattern = "*.xls") #get list of files
data.to.merge <- lapply(files, read.xls) #read in files using lapply
merged.data <- Reduce(function(...) merge(..., all = T),data.to.merge)#merge all the files

The merged.data will have data from all the sheets, and will also handle the case of files with different headers.

7
  • It doesnt work to me. The gdata package doesnt work. What i have to do with perl software to achieve it?
    – JULIAN
    Mar 14, 2014 at 20:01
  • Let me give you a positive point but help me how i can read excel files with gdata, because, i cant right now. I have windows 64-bit
    – JULIAN
    Mar 14, 2014 at 21:06
  • Are these xlsx files or xls files?
    – infominer
    Mar 14, 2014 at 21:13
  • I have some .xls files, but must are .xlsx
    – JULIAN
    Mar 14, 2014 at 21:48
  • that's why gdata doesn't work! see my post and use readWorksheetFromFile instead of gdata
    – infominer
    Mar 14, 2014 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.