如何從多個目錄中讀取多個文件到R中進行處理?

[英]How can I read multiple files from multiple directories into R for processing?


I am running a simulation study and need to process and save the results from several text files. I have the data organized in such a way where there are sub directories and within each sub directory, I need to process and get individual results for 1000 data files. This is very easy to do in SAS using macros. However, I am new to R and cannot figure out how to do such. Below is what I am trying to accomplish.

我正在進行模擬研究,需要處理並保存幾個文本文件的結果。我以這樣的方式組織數據,其中有子目錄,並且在每個子目錄中,我需要處理並獲得1000個數據文件的單獨結果。使用宏在SAS中很容易做到這一點。但是,我是R的新手,無法弄清楚如何做到這一點。以下是我想要完成的事情。

DATA Folder-> DC1 -> DC1R1.txt ... DC1R1000.txt
              DC2 -> DC2R1.txt ... DC2R1000.txt

Any help would be greatly appreciated!

任何幫助將不勝感激!

4 个解决方案

#1


8  

I'm not near a computer with R right now, but read the help for file-related functions:

我現在不在R附近的計算機附近,但是閱讀文件相關功能的幫助:

The dir function will list the files and directories. It has a recursive argument. list.files is an alias for dir. The file.info function will tell you (among other things) if a path is a directory and file.path will combine path parts.

dir函數將列出文件和目錄。它有一個遞歸的參數。 list.files是dir的別名。 file.info函數將告訴您(除其他外)路徑是否是目錄而file.path將組合路徑部分。

The basename and dirname functions might also be useful.

basename和dirname函數也可能很有用。

Note that all these functions are vectorized.

請注意,所有這些功能都是矢量化的。

EDIT Now at a computer, so here's an example:

編輯現在在計算機上,所以這是一個例子:

# Make a function to process each file
processFile <- function(f) {
  df <- read.csv(f)
  # ...and do stuff...
  file.info(f)$size # dummy result
}

# Find all .csv files
files <- dir("/foo/bar/", recursive=TRUE, full.names=TRUE, pattern="\\.csv$")

# Apply the function to all files.
result <- sapply(files, processFile)

#2


4  

If you need to run the same analysis on each of the files, then you can access them in one shot using list.files(recursive = T). This is assuming that you have already set your working directory to Data Folder. The recursive = T lists all files within subdirectories as well.

如果需要對每個文件運行相同的分析,則可以使用list.files(recursive = T)一次性訪問它們。這假設您已將工作目錄設置為數據文件夾。 recursive = T也列出了子目錄中的所有文件。

#3


2  

filenames <- list.files("path/to/files", recursive=TRUE) This will give you all the files residing under one folder and sub folders under it.

filenames < - list.files(“path / to / files”,recursive = TRUE)這將為您提供位於一個文件夾下的所有文件及其下的子文件夾。

#4


0  

You can use Perl's glob () function to get a list of files and send it to R using, e.g., RSPerl's interface.

您可以使用Perl的glob()函數來獲取文件列表,並使用例如RSPerl的接口將其發送到R.


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2011/09/11/720b3c53d6f98056852e8a3536b7ca90.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com