tg110
tg110

Reputation: 411

Aggregating fine resolution raster to coarser resolution such that pixel value of coarse pixel is proportion of unique pixel value in fine res raster

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).

enter image description here

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

Answers (1)

Robert Hijmans
Robert Hijmans

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

Related Questions