Reputation: 411
I have a raster with pixel values 1 or NA (first image below) with specifications includedr after image. I want to coarsen to 10km*10km resolution such that the new pixel value in the final coarse pixel is proportion of pixels with value=1 (last image below).
raster1
class : SpatRaster
dimensions : 33301, 32538, 1 (nrow, ncol, nlyr)
resolution : 100, 100 (x, y)
extent : 7590450, 10844250, 902100, 4232200 (xmin, xmax, ymin, ymax)
coord. ref. : WGS 84 / Pseudo-Mercator (EPSG:3857)
source : spat_PDvNPPxCooNIh09_2584.tif
name : pred
min value : 1
max value : 1
How would I do this in R? I primarily use terra R package. Thank you!
Upvotes: 0
Views: 170
Reputation: 47591
Example data
library(terra)
r <- rast(nrow=2, ncol=2, vals=c(NA, 1, 1,NA))
Solution
a <- aggregate(x, 2, mean, na.rm=TRUE)
Illustration
values(a)
# lyr.1
#[1,] 0.5
Upvotes: 1