-2

I have loads of data (6500 files that I need to process and find yearly winter sums/averages/variance for. the format for my naming system is Syyyymmdd_rp. Each file is in the same file folder. I wanted to create a function that i could put a date and the statistical methods for calculations and I am having trouble starting. Keep in mind that the winter of 2014 spans back to September 2013. So essentially I want a function that I can put a start and end date into, as well as what method to calculate to rasters with (mean, sum, variance).

0
2

To select the files you could parse the numbers, but if the file names are as regular as you say (January is '01') I think you can also use their character representation ( '9' > '8' == TRUE )

library(raster)

selectedFilesFun <- function(files, start, end, fun) {
  b <- basename(files)
  b <- substr(b, 2, 9)
  i <- b > start & b < end
  f <- files[i]
  f
  # if you have the files:
  # s <- stack(f)
  # calc(s, fun)
}

# get your files
# f <- list.files()
# here simulated: 

year <- rep(2000:2002, each=12)
month <- rep(c(paste0('0', 1:9), 10:12), 4)
f <- paste0('S', year, month, '15_rp')

x <- selectedFilesFun(f, '20010505', '20020202', fun=mean)

Not the answer you're looking for? Browse other questions tagged or ask your own question.