Genevieve Gabriel
Genevieve Gabriel

Reputation: 11

Calculations are performed on rasters within a range of values in raster data in R

I want to multiply by 2 the pixel values in a raster that are greater than 10 and less than 20. I try to using "d11[10<d11<20]<-d11*2", but it fails.

Upvotes: 0

Views: 36

Answers (1)

Chris
Chris

Reputation: 2286

I'm likely the worst person to be answering raster questions, but:

library(terra)
lil_rast <- rast(matrix(1:10, nrow=10, ncol=10))
plot(lil_rast)

lil_rast2 <- ifel(lil_rast > 4 & lil_rast < 6, lil_rast *2, lil_rast)
plot(lil_rast2)

In the ifel (terra's ifelse), if the values in lil_rast are > 4 and < 6, multiply those values in lil_rast by 2, if not, do nothing. Which the plots seem to confirm.

Upvotes: 1

Related Questions