I am trying to obtain the list of files matching a full-path pattern. So far, I have used list.files() but it did not work.
Let's assume that we have the following directory organization:
results
|- A
| |- data-1.csv
| |- data-2.csv
|
|- B
|- data-1.csv
|- data-2.csv
Then the following command:
list.files(pattern='data-.*\.csv', recursive=TRUE)
will return all the files matching the pattern. This works, but the problem appears when using a full-path pattern. For instance, if I want to obtain all the CSV files from directory results/A, I could do:
list.files(pattern='results/A/data-.*\.csv', recursive=TRUE)
This does not work, though. Somehow, it seems like R is not able to use a full-path pattern as a regular expression. In this case, the solution could be to just use results/A as the base path. But in more complex problems, that cannot be done. For instance, at some point we may want to match the subdirectories containing only characters:
list.files(pattern='results/[A-Z]+/data-.*\.csv', recursive=TRUE)
Is it possible to do this in R?
UPDATE: After using ad hoc solutions for a while, I decided to stop typing the same again and again. So, I created a library for simplifying this task.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…