I don't know how to deal with big data that I had from radars.

I've have 144 images .tif per day and I need to extract a mean daily value, for each pixel, and export it in one image .tif.

I thought to stack all the 144 images, but i don´t know how to continue and how to export the result in one image.

I'd started this:

f <- list.files(getwd()) 
ras <- lapply(f,raster) 
STACK1 <- stack(ras)   ## with dimensions :1006, 804, 808824 (nrow, ncol, ncell)

Then I don't know if its OK to create a raster and do something like:

r1<- raster(ncol=804, nrow=1006)
media<-mean(STACK1, r1)

2 Answers 2


Use calc() to apply functions over a raster object, such as Raster, RasterStack or RasterBrick:

mean <- calc(STACK1, fun = mean)

If you have na values in cells, add na.rm =T:

mean <- calc(STACK1, fun = mean, na.rm = T)

Alternatively, you can use stackApply also:

mean <- stackApply(STACK1, indices =  rep(1,nlayers(STACK1)), fun = "mean", na.rm = T)

You can do this inside a loop to apply this function to each day in your radar folder:


dirs <- list.dirs("D:/R/Radar/", full.names = T, recursive = F)

means <- list()

for(i in 1:length(dirs)){
  f <- list.files(getwd()) 
  STACK1 <- stack(f)
  means[[i]] <- calc(STACK1, fun = mean, na.rm = T)

To save as .tiff, use writeRaster function from raster package:


writeRaster(x = mean, filename = "mean.tif", driver = "GeoTiff")

@Elia's answer is a complete explanation for this.

The terra package comes with a faster solution:


r_avg<- app(STACK,mean)


r_avg <- mean(STACK)
  • These two are the same function? I find this a bit confusing, same speed? Commented Nov 24, 2022 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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