Reputation: 776
I have a number of files in my folder, e.g.:
I only wish R to find "file1.txt". But if I use
list.files(pattern = "file1.txt")
R will also return the other two files from my example. Any ideas how I could solve this?
Thanks!
Upvotes: 6
Views: 4559
Reputation: 14460
Use regular expressions (see ?regex
):
list.files(pattern = "^file1\\.txt$")
^
is the regular expression denoting the beginning of the string,
\\
escapes the .
to make it a literal .
,
$
is the regular expression denoting the end of the string.
In sum, this is the regular expression capturing exactly file1.txt
and nothing else.
Upvotes: 14