Search code examples
rlistfunctionrasterlapply

Sampling with list of rasters and locations using function in R


I am sweating over this piece of code. I have received previously help to build it here. In short, what I am doing here I have list of three rasters that I am randomly sampling numberv times. Therefore, the output is a list of four lists, each list has three rasters. After I obtain the random points locations, I then take the raster value in this location. Problem I want to solve is that I would like to take the second sample locations, ie sample.set[[1]][2] and obtain raster value from rasters[1]. Then I would like to take sample.set[[1]][3] and obtain raster value from rasters[2]. Then sample.set[[2]][2] and obtain raster value from rasters[1] and sample.set[[2]][3] and obtain raster value from rasters[2] etc. The result would be a list of 4 lists, each list with 2 elements with sample xy values (locations) and previous raster value. Help will be much appreciated.

y <- matrix(1:150,50,3)
mv <- c(1,2,3)
rep = 20

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)

numberv = c(10,12,14,16)        # sample number vector 

# 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)

Now I would like to use the sample values from the second element of the list sample.set to sample 1st raster in list rasters I try this but no success:

sample.extract.prev <- function(x) {
        lapply(1:length(x),function(y) data.frame(x[[y]],
                                                  extract(rasters[[y]],x+1[[y]])))
}


sample.values.prev <- lapply(sample.set,sample.extract.prev)

Solution

  • Managed to solve this (big high five to myself ;) Unfortunately I managed to do it with a loop, would be great to see an example of a function.

    samplevaluesnext <- vector("list",length(sample.set))
    
    ## Look up values
    for (j in 1:length(sample.set)) {
            for (i in 1:(length(rasters)-1)) {
                    samplevaluesnext[[j]][[i]] <- data.frame(sample.set[[j]][[i+1]],
                                                             extract(rasters[[i]], 
                                                                     as.data.frame(sample.set[[j]][i+1])))
            }
    }