使用R中的功能对栅格和位置列表进行采样 [英] Sampling with list of rasters and locations using function in R

查看:101
本文介绍了使用R中的功能对栅格和位置列表进行采样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为这段代码大汗淋漓.我以前在此处上得到过帮助.简而言之,我在这里所做的事情是我随机采样numberv次的三个栅格的列表.因此,输出是四个列表的列表,每个列表都有三个栅格.在获得随机点位置之后,我便在该位置获取栅格值. 我要解决的问题是我想获取第二个采样位置,即sample.set[[1]][2]并从rasters[1]获取栅格值.然后,我想使用sample.set[[1]][3]并从rasters[2]获取栅格值.然后sample.set[[2]][2]并从rasters[1]sample.set[[2]][3]获得栅格值,并从rasters[2]等获得栅格结果.结果将是4个列表的列表,每个列表包含2个具有样本xy值(位置)的元素,并且前一个栅格值. 帮助将不胜感激.

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)

现在,我想使用列表sample.set的第二个元素中的样本值来采样列表rasters中的第一个栅格,我尝试这样做但没有成功:

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)

推荐答案

设法解决了这个问题(对我来说是高五分;) 不幸的是,我设法通过循环实现了这一点,很高兴看到一个函数示例.

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])))
        }
}

这篇关于使用R中的功能对栅格和位置列表进行采样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆