繁体   English   中英

如何将几个 excel 文件与 R 中的多个工作表合并?

[英]How to consolidate several excel files with several sheets in R?

我想将 7 个 excel 文件与 R 中的几张纸合并。 所有工作表都具有相同的结构。 我尝试使用 for 但结果是最后一个工作簿或错误。 代码是:

files <- list.files(pattern = ".xlsx")
sheets <- excel_sheets(files)
library(xlsx)
setwd("C:/Users/User/Documents")

for(i in 1:7){
  file <- files
  vari <- sheets %>%
    set_names() %>%
    map_df(~ read_excel(path,skip = 5 ,sheet = .x), .id = "sheet")
}

谢谢...

我会提供这样的解决方案。 您可以使用其中的文件和工作表列表组成一个data.frame 然后使用map2_dfr

library(tidyverse)
setwd("C:/tmp") 

path <- list.files(".", pattern = ".xlsx", full.names = T)

df_files <- data.frame(files = path) %>% 
  rowwise() %>% 
  mutate(sheets = list(excel_sheets(files))) %>% 
  unnest(sheets)

df <- map2_dfr(.x = df_files$files, .y = df_files$sheets, readxl::read_xlsx)

暂无
暂无

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

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