fffpinas
fffpinas

Reputation: 61

How to select multiple files with different extension in filelist in R

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)

Upvotes: 6

Views: 1176

Answers (1)

akrun
akrun

Reputation: 887213

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)

Upvotes: 7

Related Questions