尝试从同一目录中读取多个.xlsx文件到R时遇到问题。我一直收到以下错误。
" path.expand(文件)出错:参数"文件"缺少,没有默认"
我的代码如下。
require(.xlsx)
Files=list.files(path="I:/Marcs_Discretinization_try_1/Attempt1/Actual Data", pattern=".xlsx")
sapply(Files, read.xlsx2(sheetIndex=8))
对象文件的输出看起来像这样,似乎没有附加路径。
[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"
答案 0 :(得分:1)
您想合并文件夹中的所有Excel文件吗?
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))
}
答案 1 :(得分:1)
我通常使用 lapply
和 bind_rows
。舒适快捷。
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()