lundi 29 octobre 2018

Specific pattern to list files in R

I have an issue when matching a very specific pattern when using list.files. I have a set of files with this pattern "namefile_YYYY-mm-dd.csv", but also, if a file was generated more than one time then I will have something like this: "namefile_YYYY-mm-dd_something-else.csv". This is what I used:

list.files(path = "\\projects\\datasets", pattern = "^Participants_(.*)csv$",all.files = FALSE, full.names = FALSE, recursive = FALSE)

This is the output: "Participants_2018-07-18.csv" "Participants_2018-07-19.csv" "Participants_2018-07-21.csv" "Participants_2018-07-28.csv" "Participants_2018-08-04 new.csv" "Participants_2018-08-04.csv" "Participants_2018-08-11.csv" "Participants_2018-08-18 - for-analysis.csv" "Participants_2018-08-18.csv" "Participants_2018-08-25.csv" "Participants_2018-09-01.csv" "Participants_2018-09-08.csv"

But I don't want all of them. I would like the list includes only the names in this format "Participants_2018-07-18.csv" and exclude the names in any other format like this one: "Participants_2018-08-04 new.csv", "Participants_2018-08-18 - for-analysis.csv"

I tried several things like

list.files(path = "\\projects\\datasets", pattern = "^Participants_[0-9]\\.csv$",all.files = FALSE, full.names = FALSE, recursive = FALSE)]

But the list came empty. Any advise?

Aucun commentaire:

Enregistrer un commentaire