I have a set of 16 folders, each of one having sub-folders. I want a code to go into each of those 16 folders, check all the sub-paths and import only those files that fulfill the following conditions: either they contain "B02.jp2" OR "B03.jp2" OR "B04.jp2" OR "B08.jp2"
Here is a screenshot of the files I want to select
Here is the code I am using so far:
This one works perfectly but it only uses one condition (B08.jp2$)
path <- "my/path/"
path <- list.files(path, recursive = TRUE, full.names = FALSE, pattern = "B08.jp2$")
I have been trying to combine multiple patterns but without success: here are my attempts
Based on: list.files pattern argument in R, extended regular expression use
path <- "my/path/"
path <- list.files(path, recursive = TRUE, full.names = FALSE, pattern = glob2rx("B08.jp2$*B03.jp2$"))
path<-character(0)
Based on: R list files with multiple conditions
path <- "my/path/"
path <- list.files(path, recursive = TRUE, full.names = FALSE, pattern = "B08.jp2$ | B03.jp2$")
path<-character(0)
-- EDIT --
I have change my data slightly and would like to import them in a different way. My files are now called:
B02_10m.jp2
B03_10m.jp2
B04_10m.jp2
B08_10m.jp2
B05_20m.jp2
B06_20m.jp2
B07_20m.jp2
B8A_20m.jp2
B11_20m.jp2
B12_20m.jp2
They are located in different sub-folders. That's way I am using recursive=TRUE
.
I have trying with the following options to combine the conditions but it's not working.
S2 <- "my/path"
S2 <- list.files(S2, recursive = TRUE, full.names = TRUE, pattern = "B0[2348]_10m.jp2$ | B(0[567]_20m)|(1[12]_20m)|(8A_20m).jp2$")
S2 <- "my/path"
S2 <- list.files(S2, recursive = TRUE, full.names = TRUE, pattern = "B0[2348]_10m | B(0[567]_20m)|(1[12]_20m)|(8A_20m).jp2$")
Try this:
list.files(path, recursive = TRUE, full.names = FALSE,
pattern = "B0[2348].jp2$")
The pattern accepts a regular expression.