从不同目录导入多个文件(但在每种情况下都具有相似的结构) [英] Import several files from different directory (but similar structure in every case)
本文介绍了从不同目录导入多个文件(但在每种情况下都具有相似的结构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是R世界的新手,所以我有一个问题:
I'm new in the R world, so I have this question:
- 我有一个主体文件夹。在该文件夹中,我有几个子文件夹,然后在另一个子文件夹中,还有另一个子文件夹。例如:
Works /文件夹A,B,C ... / Economic / New / File.xlsx
Works/Folder A,B,C.../Economic/New/File.xlsx
此结构在文件夹A,B,C,D和其余文件夹中重复。
我需要的是导入每个 file.xlsx并将所有数据帧合并为一个。
This structure repeats for Folder A, B, C, D, and the rest. What I need is import every "file.xlsx" and merge all dataframes into one.
非常感谢!
推荐答案
使用 map_df
, list.files
。假设您的文件都将其数据保存在第一个工作表上。
An approach using map_df
, list.files
. Assuming that your files all hold their data on the first worksheet.
library(tidyverse)
myConcat <-
list.files("Economic/New", recursive = TRUE, pattern =
"(?i)file.xlsx", full.names=TRUE) %>%
map_df( ~ readxl::read_excel(.x, sheet = 1))
# Runs and works on my own files, (with different directories and names, of course.)
(?i)
使其不区分大小写;我注意到您同时拥有 File.xlsx 和 file.xlsx 。
The (?i)
makes it case insensitive; I noticed you had both File.xlsx and file.xlsx.
这篇关于从不同目录导入多个文件(但在每种情况下都具有相似的结构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文