Question

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?

Was it helpful?

Solution 4

 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.

OTHER TIPS

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

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.

Here it is:

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

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top