简体   繁体   English

从R中不同文件夹中的Excel文件导入图纸

[英]Import sheets from Excel files located in different folder in R

Basically I have two Excel files with the same name "Checklist" in two different folder (one is 2018 and the other one is 2019). 基本上,我在两个不同的文件夹中有两个具有相同名称“ Checklist”的Excel文件(一个是2018,另一个是2019)。 Checklist has different sheets, one for each month : "January", "February" etc... Of course, all the sheets have exactly the same variables. 清单有不同的工作表,每个月一个:“一月”,“二月”等...当然,所有工作表的变量都完全相同。 I would like to put in the same data frame all the sheets from both Excel files. 我想将两个Excel文件中的所有工作表放在同一数据框中。 For now, I can gather the sheets from one Excel File with : 现在,我可以使用以下命令从一个Excel文件中收集工作表:

library(readxl)   
library(tibble)
read_excel_allsheets <- function(filename, tibble = TRUE) {
  sheets <- readxl::excel_sheets(filename)
  x <- lapply(sheets, function(X) readxl::read_excel(filename, sheet = X))
  if(!tibble) x <- lapply(x, as.data.frame)
  names(x) <- sheets
  x
}

mysheets <-read_excel_allsheets("C:/Users/Thiphaine/Documents/2018/Checklist.xlsx")
library(dplyr)
mysheets<-bind_rows(mysheets, .id = "column_label")

I just don't know how to create a loop that will go through the folder 2018 and 2019 to gather all the sheets from both Excel file. 我只是不知道如何创建一个循环,该循环将穿过文件夹2018和2019从两个Excel文件中收集所有工作表。 The idea will also be that in 2020, I will have another folder "2020" that should be included... Any idea? 这个想法还将是,到2020年,我将有另一个文件夹“ 2020”应包括在内...有任何想法吗? Thanks 谢谢

Try this: 尝试这个:

library(dplyr)
allsheets <- list()
for(file in list.files(path = "PATH/TO/DCUMENTS/", 
    recursive = TRUE, pattern = "*.xlsx", full.names = TRUE)) {
  mysheets <- read_excel_allsheets(file)
  mysheets <- bind_rows(mysheets, .id = "column_label") 
  allsheets[[file]] <- mysheets
}

where PATH/TO/DOCUMENTS is probably something like "C:/Users/Thiphaine/Documents/ for you. 其中PATH/TO/DOCUMENTS可能类似于"C:/Users/Thiphaine/Documents/ ”。

If you'd like can also vectorize it using the tidyverse approach. 如果您愿意,也可以使用tidyverse方法对其进行矢量化。 Especially because all of your files are the same (column names) and you want to end up with a data.frame . 特别是因为所有文件都是相同的(列名),并且您最终要使用data.frame

require(tidyverse)

df <- list.files(path = "your_path",
                       full.names = TRUE,
                       recursive = TRUE,
                       pattern = "*.xls") %>% 
tbl_df() %>%
mutate(sheetName = map(value, readxl::excel_sheets)) %>%
unnest(sheetName) %>% 
mutate(myFiles = purrr::map2(value, sheetName, function(x,y) {
    readxl::read_excel(x, sheet = paste(y))})) %>% 
unnest(myFiles)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM