MockCommunity1
MockCommunity1

Reputation: 31

How to read in multiple ".xlsx" files to R

Having trouble trying to read in multiple .xlsx files to R from the same directory. I keep getting the following error.

"Error in path.expand(file) : argument "file" is missing, with no default"

My code is as follows.

require(.xlsx)
Files=list.files(path="I:/Marcs_Discretinization_try_1/Attempt1/Actual     Data", pattern=".xlsx")
sapply(Files, read.xlsx2(sheetIndex=8))

The output of object Files looks like this which seemingly does not have the attached path.

 [1] "2015-B1-2OR.xlsx"    "2015-B1-OR10-B.xlsx" "2015-B1-OR10.xlsx"   "2015-B1-OR19.xlsx"   "2015-B2-OR19.xlsx"  
 [6] "2015-O1-2OR.xlsx"    "2015-O1-OR10-B.xlsx" "2015-O1-OR10.xlsx"   "2015-O2-2OR.xlsx"    "2015-O2-OR10-B.xlsx"
[11] "2015-O2-OR10.xlsx"   "2015-X1-2OR.xlsx"    "2015-X1-OR10-B.xlsx" "2015-X1-OR10.xlsx"   "2015-X2-2OR.xlsx"   
[16] "2015-X2-OR10-B.xlsx" "2015-X2-OR10.xlsx"  

Upvotes: 1

Views: 16744

Answers (2)

ToWii
ToWii

Reputation: 670

I use a lapply and bind_rows usually. Comfortable and fast.

require(tidyverse)
require(magittr)
require(readxl)

path <- "I:/project/raw_data"
  
url_xlsx <- list.files(path, pattern = "*.xlsx", recursive = TRUE)
  
read_xlsx_files <- function(x){
   df <- read_xlsx(path = paste(path, x, sep = "/"))
   return(df)
 }
  
df <- lapply(url_xlsx, read_xlsx_files ) %>%
   bind_rows()

Upvotes: 2

ASH
ASH

Reputation: 20352

You want to merge all Excel files in a folder?

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

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

Upvotes: 3

Related Questions