Home > Net >  using list.files(pattern =) to import files from a vector, R
using list.files(pattern =) to import files from a vector, R

Time:09-02

Using R, I'm trying to import a list of files into my environment in RStudio with help of a numerical vector that's updated by other functions in the script but the pattern argument specified in list.files() is partly ignored. I have

test_list <- list.files(
                    path='~/somepath/'
                    , pattern = paste0(threshold_list, "\\.csv$")
                    , recursive = TRUE
                    , full.names = TRUE)

where threshold_list is vector consisting of integers that refer to a specific csv file,

[1]   1   2   3   4   5   6   7   8   9  10 ...

so, if I were to print "paste0(threshold_list, "\\.csv$")", I get the file names I want:

"\\1.csv$"   "\\2.csv$"   "\\3.csv$"   "\\4.csv$" ... "\\150.csv$"

But, if I pass this vector to the pattern argument in list.files() and then execute the script, I will get the files in intervals of 10 which is not what I need;

 [1] ".../somepath/001.csv"
 [2] ".../somepath/011.csv"
 [3] ".../somepath/021.csv"
 [4] ".../somepath/031.csv"
 ...
 [n] ".../somepath/141.csv"

If the first component in the vector is e.g. 34, the first file will be "034.csv", and the same pattern will occur. When list.files(pattern="*.csv") all files within the directory are imported into the environment so I'm assuming the issue is with my regular expression in the pattern argument. I want the list to include every file name that's specified by "paste0(threshold_list, "\\.csv$")", how can I achieve this?

Edit 1: file names were actually 001.csv, 002.csv etc, converting the vector so that that all of it's components are three digit numbers,

sprintf("d", threshold_list)

and applying the given answer below, achieves the solution.

CodePudding user response:

you don't use wildcards in regexp patterns, you should use class enumerations in square brackets

test_list <- list.files(path='~/somepath/', pattern = sprintf('%s\\.csv$', paste(threshold_list, sep='', collapse='|')), recursive = TRUE, full.names = TRUE)
  • Related