vendredi 4 février 2022

Manipulating raster values - increase its values by a randomly selected ratio from a range in R

I am working with a very large raster. I want to increase values in each pixel randomly by 0.3-0.5 of its original value. What kind of loop should I apply to achieve it elegantly?

Example raster built below. My raster is a .tif, and I would prefer not convert it to matrix first, unless it is the best solution?

library(raster)  

## Create a matrix with random data & use image()

xy <- matrix(rnorm(400),20,20)
image(xy)

# Turn the matrix into a raster
rast <- raster(xy)
# Give it lat/lon coords for 36-37°E, 3-2°S
extent(rast) <- c(36,37,-3,-2)
# ... and assign a projection
projection(rast) <- CRS("+proj=longlat +datum=WGS84")
plot(rast)



Aucun commentaire:

Enregistrer un commentaire