I need to select files which start with "M" and end with ".csv". I can easily select files which start with "M" : list.files(pattern="^M"), or files which end with "csv": list.files(pattern = ".csv"). But how to select files which satisfy both conditions at the same time?
You could try glob2rx
lf <- list.files("path_to_directory/", pattern=glob2rx("M*.csv"))
which translates to:
glob2rx("M*.csv")
[1] "^M.*\\.csv$"
The pattern
argument takes a regular expression:
list.files(pattern='^M.*csv')
To be more specific, your second expression:
list.files(pattern='.csv')
Is matching all files with the string csv
preceded by any character. To be explicit and only match files with a .csv
extension:
list.files(pattern='\\.csv$')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With