R:如何选择目录中同时满足名称开头和结尾条件的文件? [英] R: How to select files in directory which satisfy conditions both on the beginning and end of name?

查看:37
本文介绍了R:如何选择目录中同时满足名称开头和结尾条件的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要选择以"M"开头并以" .csv"结尾的文件.我可以轻松选择以"M"开头的文件:list.files(pattern ="^ M"),或以"csv"结尾的文件:list.files(pattern =" .csv").但是,如何同时选择同时满足这两个条件的文件?

I need to select files which start with "M" and end with ".csv". I can easily select files which start with "M" : list.files(pattern="^M"), or files which end with "csv": list.files(pattern = ".csv"). But how to select files which satisfy both conditions at the same time?

推荐答案

您可以尝试 glob2rx

lf <- list.files("path_to_directory/", pattern=glob2rx("M*.csv"))

其翻译为:

glob2rx("M*.csv")
[1] "^M.*\\.csv$"

这篇关于R:如何选择目录中同时满足名称开头和结尾条件的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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