Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Include pattern in list.dirs

Tags:

directory

loops

r

surely a very newbish question, but how do I include a pattern inside a list.dirs function?

For example, list.files function

Imagery=list.files(full.names=TRUE, recursive=TRUE, pattern= "*20m*.tif$")

returns all the files that have 20m in their name and have .tif as extension.

But when i try to apply this logic to list.dirs

directories=list.dirs(full.names = TRUE, recursive=TRUE, pattern="R10m" )

i get this error:

Error in list.dirs(full.names = TRUE, recursive = TRUE, pattern = "R10m") : 
unused argument (pattern = "R10m")

Hope I am not missing something obvious here. My goal is to get the full path of all directories that have a folder named "R10m". I have a lot of folder that have many subdirectories, and most of them have similar structure. I would like to list only those that have this folder, and within them list all files that are tifs. I know I can get the files I need with only list.files options, but I need the directory path and file names later as variables.

Thank you beforehand for your time,

Best regards, Davor

like image 205
davor korman Avatar asked Oct 26 '17 17:10

davor korman


1 Answers

Three alternatives:

dirs <- list.dirs()
dirs <- dirs[ grepl(your_pattern, dirs) ]

or

dirs <- list.dirs()
dirs <- grep(your_pattern, dirs, value = TRUE)

or

files <- list.files(pattern = your_pattern, recursive = TRUE, include.dirs = TRUE)
dirs <- files[ file.info(files)$isdir ]
like image 194
r2evans Avatar answered Oct 31 '22 08:10

r2evans