读取r中不同目录中的几个文件 [英] Read several files in different directories in r

查看:137
本文介绍了读取r中不同目录中的几个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从differents目录中读取多个.csv文件,然后将其放在单个数据框中.

I want to read multiple .csv files from differents directories then put it in a single dataframe.

我有两种目录可供读取:

I have two kinds of directories to read:

A:/LogIIS/FOLDER 01 /" files.csv "

A:/LogIIS/FOLDER01/"files.csv"

在其他地方,有一个包含几个files.csv的文件夹,例如下面的示例:

On others there a folder with several files.csv, as the example bellow:

A:/LogIIS/FOLDER 02 /FOLDER_ A /" files.csv

A:/LogIIS/FOLDER02/FOLDER_A/"files.csv

"A:/LogIIS/FOLDER 02 /FOLDER_ B /" files.csv "

"A:/LogIIS/FOLDER02/FOLDER_B/"files.csv"

"A:/LogIIS/FOLDER 02 /FOLDER_ C /" files.csv "

"A:/LogIIS/FOLDER02/FOLDER_C/"files.csv"

"A:/LogIIS/FOLDER 03 /FOLDER_ A /" files.csv "

"A:/LogIIS/FOLDER03/FOLDER_A/"files.csv"

"A:/LogIIS/FOLDER 03 /FOLDER_ B /" files.csv "

"A:/LogIIS/FOLDER03/FOLDER_B/"files.csv"

"A:/LogIIS/FOLDER 03 /FOLDER_ C /" files.csv "

"A:/LogIIS/FOLDER03/FOLDER_C/"files.csv"

"A:/LogIIS/FOLDER 03 /FOLDER_ D /" files.csv "

"A:/LogIIS/FOLDER03/FOLDER_D/"files.csv"

提前谢谢!

推荐答案

我还将使用list.files()函数和循环来提取所有信息.列出通用顶级目录下的所有目录,在本例中为目录A:/LogIIS

I would also use the the list.files() function with a loop to extract all information. list all directories under the common top level directory in this case the directory A:/LogIIS

common_path = "A:/LogIIS/"
primary_dirs = list.files(common_path);
primary_dirs 
[1] "FOLDER01" "FOLDER02" "FOLDER03"

现在我将对所有primary_dirs进行嵌套循环,在您的示例中,所有.csv文件都有一个通用名称files.csv,这简化了问题,您也没有说过如何添加csv文件,但是我将假定它们具有相同的列标题,并将使用cbind()附加它们,否则可以使用rbind()

now I would do a nested loop over all primary_dirs, in your example all the .csv files have a common name files.csv which simplifies the problem, you also haven't said how to append the csv files but I will assume they have the same column headers and will use cbind() to append them, otherwise you could use rbind()

main_data = data.frame(##populate heade) ## 

使用此处

for(dir in primary_dirs) {
  sub_folders = list.files(paste(common_path,dir,sep = ""))
  if (any(sub_folders %in% "files.csv")) {
    ## there is files.csv in this directory read it in and append to a data.frame.
    ## read in data 
    temp_data = read.csv(file = paste(common_path,dir,"/files.csv",sep = ""))
    ## append
    main_data = cbind(main_data,temp_data);
  } else {
    ## try go one more directory deeper
    for(sub_dir in sub_folders) {
      sub_sub_files = list.files(paste(common_path,dir,"/",sub_dir,sep = ""))             
      if (any(sub_sub_files %in% "files.csv")) {
        ## found files.csv read it in and append it
        temp_data = read.csv(file = paste(common_path,dir,"/",sub_dir,"/files.csv",sep = ""))
        main_data = cbind(main_data,temp_data);
      } else {
        warning("could not find the file 'files.csv' two directories deep")
      }
    } 
  }
}

这篇关于读取r中不同目录中的几个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆