Home calcul of the mean of every 6 rasters layer and name of this files

# calcul of the mean of every 6 rasters layer and name of this files

user5388
1#
user5388 Published in September 20, 2018, 10:38 pm

I have 28 rasters files combined in a folder. I would like to do the mean of the 3 first rasters, then the mean of the 3 other raster, etc. Then, I should obtain 9 rasters. I would like to name them according to the month. I would like my first raster layer would be named Tmin-2010-01-15, my second one 'Tmin-2010-02-15', ..., and my last and nine raster called 'Tmin-2010-09-15'. I wrote the following code.I do not have any error message but I cannot see any results so I do not know if the logic of the code is good and how to process.

Thanks for any help!

setwd("/Users/lisemontefiore/Documents/Data/Climate Data/test2")


I stacked my rasters

bands <-list.files(path="xxx",pattern="*.tif",full.names=TRUE)
data_stack <- stack(bands)


I define the parameters n=number of raster layers, m=number of rasters I want to gather to do the mean

n<- 28
m<-3
groupn=function(n,m){rep(1:m,rep(n/m,m))}
group3=groupn(28, 3)
f=function(v){tapply(v, group3, mean)}
b=calc(data_stack, f)


I define the name of my files created by the function and write the rasters

    months<- seq(from=as.Date("2010-01-15"),
to= as.Date("2010-09-15"),
by='month')
for (i in seq_along(months))
{writeRaster <- file.path("b",
paste('Tmin',months[i], sep=''),
"GTiff")