TransWikia.com

Extracting mean of multiple raster layers using R?

Geographic Information Systems Asked by Christopher D on January 24, 2021

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:

set("D:/R/Radar/Rasday1")
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)

One Answer

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:

library(raster)

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

means <- list()

for(i in 1:length(dirs)){
  setwd(dirs[i])
  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:

library(raster)

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

Answered by aldo_tapia on January 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP