Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files in R that do NOT match a pattern

Tags:

regex

r

R has a function to list files in a directory, which is list.files(). It comes with the optional parameter pattern= to list only files that match the pattern.

Files in directory data: File1.csv File2.csv new_File1.csv new_File2.csv

list.files(path="data", pattern="new_")

results in [1] "new_File1.csv" "new_File2.csv".

But how can I invert the search, i.e. list only File1.csv and File2.csv?

like image 860
MERose Avatar asked Jul 23 '15 14:07

MERose


2 Answers

I belive you will have to do it yourself, as list.files does not support Perl regex (so you couldn't do something like pattern=^(?!new_)).

i.e. list all files then filter them with grep:

grep(list.files(path="data"), pattern='new_', invert=TRUE, value=TRUE)

The grep(...) does the pattern matching; invert=TRUE inverts the match; value=TRUE returns the values of the matches (i.e. the filenames) rather than the indices of the matches.

like image 106
mathematical.coffee Avatar answered Oct 06 '22 00:10

mathematical.coffee


I think that the simplest (and probably fastest if you include programmer time) approach is to run list.files 2 times, once to list all the files, then the second time with the pattern of files that you do not want, then use the setdiff function to find those file names that are not in the group that you want to exclude.

like image 22
Greg Snow Avatar answered Oct 06 '22 00:10

Greg Snow