regex - List files in R that do NOT match a pattern -
r
has function list files in directory, list.files()
. comes optional parameter pattern=
list files match 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 invert search, i.e. list file1.csv
, file2.csv
?
i belive have yourself, list.files
not support perl regex (so couldn't pattern=^(?!new_)
).
i.e. list files filter them grep
:
grep(list.files(path="data"), pattern='new_', inv=t, value=t)
the grep(...)
pattern matching; inv=t
inverts match; value=t
returns values of matches (i.e. filenames) rather indices of matches.
Comments
Post a Comment