如何将多个目录中的多个文件读入R进行处理? [英] How can I read multiple files from multiple directories into R for processing?

查看:97
本文介绍了如何将多个目录中的多个文件读入R进行处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行模拟研究,需要处理和保存几个文本文件中的结果.我以这样一种方式来组织数据,即在每个子目录中都有子目录,我需要处理并获取1000个数据文件的单独结果.在SAS中使用宏非常容易做到这一点.但是,我是R的新手,无法弄清楚该怎么做.以下是我要完成的工作.

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.

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

任何帮助将不胜感激!

推荐答案

我现在不在带R的计算机附近,但请阅读文件相关功能的帮助:

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

dir函数将列出文件和目录.它具有递归参数. list.filesdir的别名. file.info函数将(除其他事项外)告诉您路径是否为目录,而file.path将合并路径部分.

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.

basenamedirname函数也可能有用.

请注意,所有这些功能都是矢量化的.

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)

这篇关于如何将多个目录中的多个文件读入R进行处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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