Omar Gonzales
Omar Gonzales

Reputation: 4008

list.files: regex pattern not working on windows?

I can access and get the path of my file with file.choose():

"D:\\amca\\reportes_mov\\data_mym\\mym_total_csv\\hhhh-2020-11-09.csv"

I'm using a project, so I don't have to type the whole path, but after reportes_mov. Why my list files is empty after I run it?

files  <- list.files(pattern = 'data_mym\\/mym_total_csv\\/hhhh-2020-[0-9]{2}-[0-9]{2}\\.csv')

Other attemp: still empty.

files  <- list.files(pattern = 'data_mym\\mym_total_csv\\hhhh-2020-[0-9]{2}-[0-9]{2}\\.csv')

Upvotes: 1

Views: 541

Answers (1)

wp78de
wp78de

Reputation: 18990

The pattern param of list.files only matches the file name part not the path:

path: a character vector of full path names; the default corresponds to the working directory, getwd()...

pattern: an optional regular expression. Only file names which match the regular expression will be returned.

And if I understand correctly, you will also need the param full.names = TRUE

If TRUE, the directory path is prepended to the file names to give a relative file path. If FALSE, the file names (rather than paths) are returned.

Try

files  <- list.files(path = 'data_lsd/lsd_total_csv', pattern = 'america-2020-[0-9]{2}-[0-9]{2}\\.csv', full.names=TRUE, ignore.case = TRUE)

Which outputs the CSVs in the subdirectory as expected:

[1] "data_lsd/lsd_total_csv/america-2020-11-09.csv"
[2] "data_lsd/lsd_total_csv/america-2020-11-10.csv"

Upvotes: 2

Related Questions