54

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?

8 Answers 8

49
file.ls <- list.files(path='~/DATA/PiP/Curvas/',pattern="20130801|USD") 
Sign up to request clarification or add additional context in comments.

5 Comments

This command filters them by currency, the result is a string with the files that contains USD in the name, but does not filter by the date.
The vertical bar means "or", hence all files are returned which either matches the date and/or the currency. How to filter on both conditions?
doesn't work, replacing with '&' yields character(0)
I am also interested on how to use the '&'. grep will do for now though.
For "&" ("and"), if you know the order, use regex like 20130801.+USD or .+20130801.+USD.+. The .+ matches any characters in-between (and possibly before/after).
41

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.

Comments

14

Here it is:

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

Comments

6
 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 Comments

It works well, but I was hoping to work it out in the pattern argument instead of adding another line, but I think yours is the best solution.
OP didn't ask for a second line to apply to the result of the first ... so it's impossible to do in one line? (not saying it's not the best solution)
6

If you want to preserve your pattern as a vector (if you are using this in a package function and want to allow the user to specify the pattern as a vector instead of having them use | or *), you can set it up like this:

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

This also allows the pattern1 vector to contain as many elements as you want without having to adjust your code.

Comments

6

If you want to use "and" (&) rather than "or" (|), you can use .+ within the pattern.

For example:

list.files(path='~/DATA/PiP/Curvas/',pattern="20130801.+USD") 

Comments

1

I interpret you are looking for an AND condition. Then I would use:

file.ls <- grep(pattern = "(?=.*20130801)(?=.*USD)", x = list.files(path = '~/DATA/PiP/Curvas/'), value = TRUE, perl = TRUE) 

I use this command to return .tex files that I have tagged with the + sign (a sort of primitive tagging system). For instance, to find files that are tagged as +neuralnet, and +style, and +set, I use:

grep("(?=.*\\+style)(?=.*\\+neuralnet)(?=.*\\+set)", list.files("./src", pattern = "*.tex$", full.names = TRUE), value = TRUE, perl = TRUE) 

where (?=) is the positive look ahead, and \\+ is used to escape the + sign, which is the character I use for tagging the files. You may add as many (?=...) as AND conditions as you need.

My .tex files are Latex TikZ files. This is a partial list:

 [1] "./src/block_diagram-multilayer_perceptron+neuralnet+style+learn.tex" [2] "./src/block_diagram-perceptron+neuralnet+set+learn.tex" [3] "./src/discriminator+neuralnet+matrix+foreach+style.tex" [4] "./src/foreach-neural_network-1h+neuralnet.tex" [5] "./src/generative_adversarial_network_manual_net+neuralnet.tex" [6] "./src/generator+neuralnet+matrix.tex" [7] "./src/hopfield_auto_net+neuralnet+foreach+scope+learn+style+command.tex" [8] "./src/ml_1h_manual_net+neuralnet+style+matrix+foreach.tex" [9] "./src/ml_2h_manual_net-color+neuralnet+set+foreach.tex" [10] "./src/ml_a3c_manual_net_arr+neuralnet.tex" [11] "./src/ml_auto_net_arr+neuralnet+foreach+style+foreach.tex" [12] "./src/ml-auto_net_4h_arr+neuralnet+matrix+foreach+style+scope.tex" [13] "./src/ml-auto_net_bias_arr+neuralnet+learn+foreach+def+command+ifnum+style.tex" [14] "./src/ml-auto_net_color+neuralnet+foreach.tex" [15] "./src/ml-auto_net_icon+neuralnet+style+foreach+set+function+learn.tex" [16] "./src/ml-SVM_manual+neuralnet.tex" [17] "./src/nn-01-2_summarized+neuralnet+style+learn.tex" [18] "./src/nn-02_auto_net+neuralnet+foreach+pgf+style+learn.tex" [19] "./src/nn-03_auto_net+neuralnet+foreach+style+learn.tex" [20] "./src/nn-04_auto_net+neuralnet+matrix+style+foreach.tex" [21] "./src/nn-05_auto_net_arr+neuralnet+style+foreach+learn.tex" [22] "./src/nn-06_manual_net_color+neuralnet+foreach+style.tex" [23] "./src/nn-08-tkz-berge-01+neuralnet+scope+foreach+pkg.tex" [24] "./src/nn-09_manual_net+neuralnet+foreach+scope.tex" [25] "./src/stacked_blocks+neuralnet+3d+def+pgf+set+style.tex" 

Then, by using grep, with list.files, and the regex positive look ahead (?=...), I get an effective way of selecting TikZ files by purpose or activity I want to work on. The character + works fine for tagging, in R and Linux. In Linux I use find along with -and and -regex switches.

Comments

0

Suggestion for selecting files with '20130801' and 'USD':

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.