First I create a discrete distribution from a raster. I then need to randomly pull values from that distribution and assign them to a new raster of unequal columns, rows and cells.
library(raster)
#Read-in raster to sample
#dimensions: 3945, 2772, 10935540 (nrow, ncol, ncell)
#cell size: 30m
raw_raster <- raster("X:\\path to raster...")
#Read-in a constant raster to assign new values
#This raster has unequal rows, columns and cells then the raw raster
#dimensions: 826, 680, 561680 (nrow, ncol, ncell)
#cell size: 240m
constant_raster <- raster("X:\\path to raster...")
#Pull 10,000 random values from raw raster
random_sample <- sampleRandom(raw_raster, size = 10000, asRaster = TRUE)
#Create histogram for visulations
hist(random_sample)
#Randomly assign the random sample as new values to constant raster
values(raw_raster) <- sample(random_sample)
I end up getting an error in setValues(x, value): length(values) is not equal. How do I make a sample of equal length so it will play nice with assigning values to my new raster?
In other words, I need a way to assign values to a new raster using values pulled randomly from a discrete distribution, making sure everything is of equal lengths. I can not use runif, because it pulls from a normal distribution.
Cheers.
Aucun commentaire:
Enregistrer un commentaire