2

I have a folder with a number of subdirectories with numerous files of different types. I only need to select files that have the following extension *.txt and *.shp. I have tried to use pattern with & and | combination but it does not seem to work. the | operator only selects the file format that was written last (in the code below, it only selects the *.shp files and not the *.txt)

filelist <- list.files(path = ".",pattern = '*.txt$ | *.shp$', recursive = TRUE, ignore.case = TRUE, include.dirs = TRUE, full.names = TRUE)

fffpinas
  • 21
  • 1

1 Answers1

2

We can change the pattern by escaping the dot (\\.) followed by 'txt' or 'shp' at the end ($) of the string

filelist <- list.files(path = ".",pattern = '.*\\.(txt|shp)$',
   recursive = TRUE, ignore.case = TRUE, include.dirs = TRUE, full.names = TRUE)
akrun
  • 874,273
  • 37
  • 540
  • 662