0

I did some research on this before posting and I came up with this script (which doesn't work):

library(gdata)
setwd("C:/Users/rshuell001/Desktop/excel_files")
data.files = list.files(pattern = "*.xls") 

files<-data.files

#get list of files
data.to.merge <- lapply(files, read.xls) #read in files using lapply

After the data.to.merge, I get this error:

Error in findPerl(verbose = verbose) :

How can I get this to work?

halfer
  • 19,824
  • 17
  • 99
  • 186
ASH
  • 20,759
  • 19
  • 87
  • 200
  • 1
    That's not the whole error message. What's the rest? – Rich Scriven Oct 06 '15 at 17:31
  • 1
    You might want to try another package for reading `xls`-files. For some inspiration see [this answer](http://stackoverflow.com/a/32888918/2204410). – Jaap Oct 06 '15 at 17:45
  • Thanks a lot guys!! The code below will read the first sheet in all the files. However, I want to read all sheets in all files and merge everything. – ASH Oct 06 '15 at 18:49
  • library(readxl) setwd("C:/Users/rshuell001/Desktop/excel_files") file.list <- list.files(pattern='*.xls') df.list <- lapply(file.list, read_excel) – ASH Oct 06 '15 at 18:51

1 Answers1

0

The answer will depend on how you want your data to look. If you use lapply, you'll get a list.

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

If you want to merge your data into a single data frame (for example, if your files all have the same format, I would suggest a for-loop

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

You can swap out rbind for merge depending on the contents of your files.

Setter
  • 76
  • 3