0

I am working on a raster image which is of 120m resolution. I need to make the raster divided into grids or windows of size 2 by 2 and then take the mean of the values in the grid and replace the cells by their mean by which the resolution will change to 240m (2 by 2). There should not be overlapping in the grids or window. The extremes can be padded to get the mean if the raster is having an odd number of rows or columns. The NaN values can be ignored and the results should be rasters.

How can I achieve this using R? I am quite new to it. I have converted it into a matrix and run a loop through it, Focal and sliding window and rollApply do not solve my problem.

2
  • aggregate(x, fact = 2, fun = mean, na.rm = TRUE) ## maybe?
    – mdsumner
    Jun 5, 2018 at 5:48
  • Can you post this as an answer? It worked. Thanks
    – ARU
    Jun 5, 2018 at 6:34

1 Answer 1

0

Use raster::aggregate, it's pretty close to what you describe. I think to get exact control over odd margins will require pre-padding.

library(raster)
x <- raster(matrix(1:24, 4))
y <- aggregate(x, fact = 2, fun = mean, na.rm = TRUE) 

Compare dim(x) to dim(y) and res(x) to res(y).

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.