R list files with multiple conditions

十年热恋 提交于 2019-11-29 02:29:13

问题


I want to list all files in a directory that met certain conditions (date and currency). So with only one condition the argument pattern in list.files works well:

    file.ls <- list.files(path='~/DATA/PiP/Curvas/',pattern='20130801')

For multiple conditions I've tried:

    file.ls <- list.files(path='~/DATA/PiP/Curvas/',pattern=c('20130801','USD'))

But had the same result as the first one. Is there a way to have multiple criteria in pattern argument of list.files?


回答1:


 Filter(function(x) grepl("USD", x), file.ls)

alternatively, you could construct a regular expression for pattern that only matches filenames containing both strings, but that's a wizard's game.




回答2:


file.ls <- list.files(path='~/DATA/PiP/Curvas/',pattern="20130801|USD")



回答3:


In line with Baptiste and the answer on this post (list.files pattern argument in R, extended regular expression use), you can use the following expression:

file.ls <- list.files(path='~/DATA/PiP/Curvas/',
pattern=glob2rx("*20130801*USD*"))

Where * is the wildcard.




回答4:


Here it is:

file.ls2 = intersect(list.files(pattern = "20130801"), list.files(pattern = "USD"))


来源:https://stackoverflow.com/questions/18028225/r-list-files-with-multiple-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!