从R中的目录列表创建文件列表 [英] Creating a list of files from a list of directories in R

查看:75
本文介绍了从R中的目录列表创建文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多目录的列表,每个目录内有5个文件,我想从每个目录中的这些文件中选择一个(例如,让我们说一个扩展名为.txt的目录)并编译一个这些目录. txt文件....如何创建一个从R中的目录列表中选择txt文件的循环?

I have a list of many directories, each of which have 5 files inside, from these files within each directory I want to select one (for example let us say the one with extension .txt) and compile a list of these .txt files....how do I create a loop that selects txt files from a list of directories in R?

推荐答案

您可以这样做:

dir(path = ".", pattern = "\\.txt$", full.names = TRUE, recursive = TRUE)

path是包含要查找的所有文件夹的根,pattern是与您感兴趣的文件匹配的正则表达式(在示例中,所有扩展名为.txt的文件full.names返回文件的完整路径,然后按recursive浏览path中的所有子目录.这将返回一个向量,其中包含与查询匹配的文件的完整路径.

Where path is the root that contains all the folders you want to look up, pattern is regular expression that matches the files you are interested in (in the example all the files with the .txt extension, full.names return the full path of the files, and recursiveto explore all the subfoders in path. This returns a vector with the full path for the files that match your query.

这篇关于从R中的目录列表创建文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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