Search code examples
rrandomraster

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)

Solution

  • No loops are necessary. You can access the underlying pixel data directly and simply add a set of random numbers to it:

    rast2 <- rast # a copy of the existing raster
    random_nums <- runif(length(rast2), min = 0.3, max = 0.5) # a set of random numbers the size of the image
    rast2@data@values <- rast2@data@values * random_nums # multiply the pixel data by the random values