简体   繁体   English

R-使用循环读取Excel文件并创建数据框

[英]R- Reading excel files with loop and creating data frames

I have a bunch of files (number continuously growing), each file shoud get a seperate data frame. 我有一堆文件(数量不断增长),每个文件都应该有单独的数据帧。

To simplify the reading I would like to use a Loop to read all files. 为了简化读取,我想使用循环读取所有文件。 The new data frames should be named after a string from "Filename" 新数据帧应以“文件名”中的字符串命名

In the last row i would like to create an data Frame, the Name of the new data frame should be the Content of "Filename". 在最后一行中,我想创建一个数据框,新数据框的名称应为“文件名”的内容。

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
}

If I understand correctly, you want to read many files from a list into individual data frames? 如果我理解正确,您想将一个列表中的许多文件读入单个数据帧吗? I'm offering a slightly different solution: 我提供的解决方案略有不同:

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
}

This way, each of your files is in a separate data frame , and all the data frames are in one list - results . 这样,每个文件都在一个单独的数据框中 ,所有数据框都在一个list- results To access the data frame from file "datafile1.xlsm" do this: 要从文件"datafile1.xlsm"访问数据帧,请执行以下操作:

results['datafile1']

or even 甚至

results$datafile1

What you were trying to do previously was to give a separate variable to each data frame - possible (you could construct statements with paste and then eval them, I think), but a list of dataframes is almost always a better idea. 您之前试图做的是为每个数据框提供一个单独的变量 -可能的(我认为可以用paste构造语句,然后对它们进行eval ),但是数据框列表几乎总是一个更好的主意。

Using your packages and example you can do the following: 使用您的软件包和示例,您可以执行以下操作:

Pass the filenames in a list: 在列表中传递文件名:

Namen <- list(c('fname1', 'fname2') # your names list (i am guessing without .xlsx)
einlesepfad <- "path for reading the xlsm files"

Push each file into a dataframe 将每个文件推入数据框

df.list <- list() #empty data frame list
df.list= lapply(Namen, function(i){
  fname <- paste0(einlesepfad, i, '.xlsm') # get the full filename
  x = read_excel(fname) # read the excel file in
  # Edit: Removed the name column part
  # Return your data
  return (x)
})

Edit: 编辑:
Just saw your edit with the request for named list by the elements. 刚刚看到您的编辑以及元素对命名列表的请求。 Here is a very fast (non-elegant) solution to add the names: 这是添加名称的非常​​快速(非优雅)的解决方案:

names(df.list) <- lapply(Namen, function(x){x})

You can then access every dataframe of the list by either df.list['name'] or by index df.list[1] 然后,您可以通过df.list['name']或索引df.list[1]访问列表的每个数据df.list[1]

Edit2: EDIT2:
Also, since i noticed you say your filename list is continuously growing, provided that you store all the files in the same dir you could do the following: 另外,由于我注意到您说文件名列表在不断增长,只要将所有文件存储在同一个目录中,就可以执行以下操作:

Namen <– list.files(einlesepfad, '*.xlsm') 

and just remember to change the fname inside the function and remove the '.xlsm' part as such: 只需记住在函数内部更改fname并删除'.xlsm'部分,如下所示:

fname <- paste0(einlesepfad, i)

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

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