I have thousands of .tif files which contain daily temperature data. The file names include the date in a format like this tminYYYYMMDD.tif
, here is a sample:
tmin19810707.tif
tmin19810508.tif
tmin19810314.tif
tmin19810227.tif
I only need the data from June (06), July (07) and August (08) form all those years. How can I select and use for further processing only files that are from those months?
CodePudding user response:
We can use substr
for a base R solution:
x <- c("tmin19810707.tif", "tmin19810508.tif", "tmin19810314.tif",
"tmin19810227.tif")
output <- x[substr(x, 9, 10) %in% c("06", "07", "08")]
output
[1] "tmin19810707.tif"
CodePudding user response:
You can use grep
with a regular expression indicating the pattern:
grep("\\D \\d{4}0[6-8]", x, value = TRUE)
[1] "tmin19810707.tif"
x<- c("tmin19810707.tif", "tmin19810508.tif", "tmin19810314.tif", "tmin19810227.tif")
CodePudding user response:
stole the regex from the answer from @onyambu
list.files(path = "./mypath", pattern = "\\D \\d{4}0[6-8].*\\.tif$")