Need solution, help will be much appreciated.
In the following code I am creating three rasters. I then create a random number
of point locations on this raster and I am receiving a list of three matrices with coordinates of those random locations called samples
. I then take those locations and sample raster values to receive samplevalues
.
What I want to change is that I want to create a set of 100,150,200 and 250 random point locations (numberv
). So after generating these locations and receiving a list of locations, each raster will be sampled length(numberv)
times (in this case 4 times). As I have three rasters, then I would like to obtain a list with the first element including samplevalues obtained from my three rasters sampled 100 times each, the second with raster values sampled 150 times each, etc. The list would have length(numberv)
elements. Then I would use those locations to obtain the raster values in these locations.
I pasted clean code for a simpler case with just one sample (1 element vector number
used), hope it helps.
y <- matrix(1:300,100,3)
mv <- c(1,2,3)
rep = 200
valuematrix <- vector("list",ncol(y))
for (i in 1:ncol(y)) {
newmatrix <- replicate(rep,y[,i])
valuematrix[[i]] <- newmatrix
}
library(sp)
library(raster)
rasters <- setNames(lapply(valuematrix, function(x) raster(x)),
paste0('raster',1:length(mv)))
# Create a loop that will sample the rasters
library(dismo)
number = 100 # current number for random sample points number
numberv = c(100,150,200,250) # sample number vector i want to use
# samples below will hold only coordinate values:
samples <- setNames(lapply(rasters, function(x) randomPoints(raster(x),
n=number)),
paste0('sample',1:length(mv)))
samplevalues <- vector("list",ncol(y))
for (i in 1:ncol(y)) {
samplevalues[[i]] <- data.frame(samples[[i]],extract(rasters[[i]],
samples[[i]]))
}