r - R-用循环读取excel文件并创建数据框
问题描述
我有一堆文件(数量不断增长),每个文件都应该有一个单独的数据框。
为了简化阅读,我想使用循环来读取所有文件。新数据框应以“文件名”中的字符串命名
在最后一行我想创建一个数据框,新数据框的名称应该是“文件名”的内容。
for(x in 1:nrow(Namen)) # creation of the loop
{
Filename<- Namen[x,1] #Takes the Filename from the the DF
einlesepfad <- "path for reading the xlsm files"
einlesepfad <- paste(einlesepfad,Filename,".xlsm", sep="") # creation of the path to read the xlsm file
Filename <- read_excel(einlesepfad) #The Content of "Filename" should be the Name of the new data frame
}
解决方案
如果我理解正确,您想将列表中的许多文件读入单个数据帧吗?我提供了一个稍微不同的解决方案:
results <- list()
for(x in 1:nrow(Namen)) # creation of the loop
{
Filename<- Namen[x,1] #Takes the Filename from the the DF
einlesepfad <- "path for reading the xlsm files"
einlesepfad <- paste(einlesepfad,Filename,".xlsm", sep="") # creation of the path to read the xlsm file
results[Filename] <- read_excel(einlesepfad) # The list gets a new item, named whatever value Filename had, that contains your data
}
这样,您的每个文件都在一个单独的数据框中,并且所有数据框都在一个列表中 - results
。要从文件访问数据框,"datafile1.xlsm"
请执行以下操作:
results['datafile1']
甚至
results$datafile1
您之前尝试做的是为每个数据框提供一个单独的变量- 可能(我认为您可以使用paste
然后构造语句eval
),但数据框列表几乎总是一个更好的主意。
推荐阅读
- .net - 为什么 API 调用在发布时不起作用?
- flutter - Flutter Web:HtmlElementView 和 TextArea
- r - R中3参数weibull的拐点?
- c++ - OpenCV 的 imencode 为 .jpg 提供了错误的输出
- android-studio - 如何在 android studio 中使用 volley 库获取会话 cookie?
- c# - 使用 Postman 登录 Angular ASP.NET CORE 应用程序
- python - Flask:如何将子进程脚本“连接”到运行上下文以做出响应
- flutter - 在 Google Maps Flutter 中显示/隐藏一些标记
- python - 检查列条件的 IF 语句错误
- python - 如何从熊猫数据框创建列表