# How to calculate mean values of all cells of raster files in R?

I have monthly raster (.tiff) files such as 200501.tif, 200502.tif and so on...till 202012.tif.

I want to calculate mean values of each raster (i.e. of all the cells (all rows and columns) of each raster file) (cellStats(r, 'mean')) and export them in a .csv or .xls as two columns:

``````Date     Value
Jan 2005  25
Feb 2005  36
...
...
...
Dec 2020  12
``````

I am doing it in R.

• It helps to break a question down into small units. Do you know how to compute the mean of a single raster file? Do you know how to loop over a set of files? Do you know how to write a CSV file? You can find out how to do most of these things online, and keeping questions here focussed means its easier for people to build solutions for more complex problems like yours. Apr 12, 2023 at 6:41

You can use `global` function from `{terra}` library:

``````library(terra)

r = rast()

values(r) = 1:ncell(r)

global(r, 'mean')
#          mean
# lyr.1 32400.5
``````

For a reproducible example I'm adding a list of random rasters loaded into a list:

``````library(terra)

set.seed(123)

r = rast()
rl <- sapply(1:48, function(X) setValues(r, rnorm(ncell(r))))
``````

For computing mean for every element of the raster list, use `lapply` with `global` function:

``````means = lapply(rl, function(x) global(x, 'mean'))

means = do.call(rbind.data.frame, means)
row.names(means) = paste0(rep(2005:2008,each=12),sprintf("%02d",rep(1:12,times=4)))

#                 mean
# 200501  0.0001132137
# 200502  0.0046515731
# 200503  0.0041824118
# 200504  0.0010558954
# 200505 -0.0035873522
# 200506  0.0027042357
``````

For saving the result to your machine, use `write.csv`:

``````write.csv(means, 'path/to/means.csv')
``````

Adaptation to OP code:

``````library(terra)

f = list.files(path="D:/Test/", pattern=".tiff\$", full.names=TRUE)
rl = lapply(f, function(x) rast(x))

means = lapply(rl, function(x) global(x, 'mean'))
means = do.call(rbind.data.frame, means)

write.csv(means, 'path/to/means.csv')
``````
• That's a start - for a complete answer the code would have to loop over the files, and maybe build a data frame of dates from the file name and values from the raster, to be written out using `write.csv` at the end, or write each line inside the loop using `cat`... Apr 12, 2023 at 6:39
• You're right. I edited the answer. Thank you Apr 12, 2023 at 12:53
• @aldo_tapia Thanks for your response. I have three doubts. It would be helpful if you can please clarify. I defined f <- list.files(path="D:/Test/", pattern=".tiff\$", full.names=TRUE) r <- rast(f) Is that correct? And what is X in function (X) while defining r1? because it is giving me the warning: [setValues] values were recycled. And could you please explain what is happening in sprintf("%02d",rep(1:12,times=4) Apr 12, 2023 at 16:27
• 1) I just added the adaption of your code in my answer. 2) it's a dummy argument for creating dummy rasters with random numbers. 3) `sprintf("%02d",rep(1:12,times=4)` is for adding leading zeros, that's for the reproducible example only, your names should be different than mines Apr 12, 2023 at 17:01