列出所有与R中的全路径模式匹配的文件 [英] Listing all files matching a full-path pattern in R

查看:233
本文介绍了列出所有与R中的全路径模式匹配的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取与全路径模式匹配的文件列表.到目前为止,我已经使用了list.files(),但是没有用.

I am trying to obtain the list of files matching a full-path pattern. So far, I have used list.files() but it did not work.

假设我们具有以下目录组织:

Let's assume that we have the following directory organization:

results
   |- A
   |  |- data-1.csv
   |  |- data-2.csv
   |
   |- B
      |- data-1.csv
      |- data-2.csv

然后输入以下命令:

list.files(pattern='data-.*\\.csv', recursive=TRUE)

将返回所有与模式匹配的文件.可以,但是使用全路径模式时会出现问题.例如,如果我想从目录 results/A 中获取所有CSV文件,则可以执行以下操作:

will return all the files matching the pattern. This works, but the problem appears when using a full-path pattern. For instance, if I want to obtain all the CSV files from directory results/A, I could do:

list.files(pattern='results/A/data-.*\\.csv', recursive=TRUE)

但是,这不起作用.某种程度上,R似乎无法使用全路径模式作为正则表达式.在这种情况下,解决方案可能只是使用 results/A 作为基本路径.但是在更复杂的问题中,这是无法完成的.例如,在某些时候,我们可能希望匹配仅包含字符的子目录:

This does not work, though. Somehow, it seems like R is not able to use a full-path pattern as a regular expression. In this case, the solution could be to just use results/A as the base path. But in more complex problems, that cannot be done. For instance, at some point we may want to match the subdirectories containing only characters:

list.files(pattern='results/[A-Z]+/data-.*\\.csv', recursive=TRUE)

是否可以在R中执行此操作?

Is it possible to do this in R?

更新:在使用临时解决方案一段时间后,我决定不再一次又一次键入相同的内容.因此,我创建了一个来简化此任务.

UPDATE: After using ad hoc solutions for a while, I decided to stop typing the same again and again. So, I created a library for simplifying this task.

推荐答案

首先,请注意您没有使用正则表达式模式.您的第一个示例应该是:

First, note that you are not using regular expression patterns. Your first example should be:

list.files(pattern='data-.*\\.csv', recursive=TRUE)

然后,似乎list.files内部的模式匹配已应用于文件基名(即,不包括目录路径),因此您可以将任务拆分为:

Then, it seems the pattern matching inside list.files is applied to the file basenames (i.e., not including the directory path) so you could split the task into:

  1. 查找仅与基本名称匹配的所有文件,并返回其完整路径:

  1. Find all files matching the basename only, return their full paths:

basename.matches <- list.files(pattern='data-.*\\.csv', recursive=TRUE,
                               full.names = TRUE)
basename.matches
# [1] "./results/A/data-1.csv" "./results/A/data-2.csv" "./results/B/data-1.csv"
# [4] "./results/B/data-2.csv"

  • 仅保留与预期目录匹配的内容:

  • Keep only those that match the expected directory(ies):

    full.matches <- grep(pattern='^\\./results/A/', basename.matches, value = TRUE)
    full.matches
    # [1] "./results/A/data-1.csv" "./results/A/data-2.csv"
    

  • 这篇关于列出所有与R中的全路径模式匹配的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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