I have downloaded the netcdf files of climate water deficit (def) from TerraClimate, years 1981-2010. Each raster is a monthly def for my region of interest, that being entire India.

I created a stack of 12 rasters, which is a stack of monthly def for year 1981 only and want to find the moving/rolling max value, considering a window of 3 months. I have a very straightforward single line of code-

trial<-calc(rasterstack, funtion (x) movingFun(x,3,max, circular=FALSE, na.rm=TRUE, type='around')

It works and everything. However, I do not understand how are there 12 rasters, when there should be only 10 rasters i.e. no rolling max values for 1st month Jan 1981 and last month dec 1981, since I speciefied circular=FALSE and 'around'.

Why are there 12 rasters instead of 10? Any other ways to calcualte moving/rolling max values?

1 Answer 1


If you have na.rm=TRUE then movingFun will use a smaller window at the edges:

> movingFun(1:12, 3, max,na.rm=TRUE, type="around")
 [1]  2  3  4  5  6  7  8  9 10 11 12 12

So the two 12s at the end are the max of 10,11,12 at the 11th location, and the max of 11,12,NA at the 12th location.

If you don't want those rasters in your output, subset them off.

  • Thank you very much for this! This makes sense now.
    – tg110
    Feb 6, 2020 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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