Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing loops on list of lists of rasters

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]]))
}
like image 394
MIH Avatar asked Jul 21 '15 11:07

MIH


1 Answers

Does this work?

# Function to sample using a given number (returns list of three)
sample.number <- function(x) {
  rps <- lapply(rasters, function(y) randomPoints(raster(y),n=x))
  setNames(rps,paste0('sample',1:length(mv)))
}

# Apply sample.number() to your numberv list
sample.set <- lapply(numberv,sample.number)

# Function to extract values from a given sample
sample.extract <- function(x) {
  lapply(1:length(x),function(y) data.frame(x[[y]],extract(rasters[[y]],x[[y]])))
}

# Apply sample.extract() to the set of samples (returns list of four lists)
sample.values <- lapply(sample.set,sample.extract)

# Access sample 1 of number 200
summary(sample.values[[3]][[1]])
like image 130
johnson-shuffle Avatar answered Oct 06 '22 01:10

johnson-shuffle