在栅格列表清单上执行循环 [英] Performing loops on list of lists of rasters

查看:80
本文介绍了在栅格列表清单上执行循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要解决方案,将不胜感激.

Need solution, help will be much appreciated.

在以下代码中,我将创建三个栅格.然后,我在该栅格上创建一个随机的点位置number,并且我收到三个矩阵的列表,这些矩阵的坐标为这些随机位置的samples.然后,我选择这些位置并采样栅格值以接收samplevalues.

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.

我要更改的是我要创建一组100,150,200和250个随机点位置(numberv).因此,在生成这些位置并接收到位置列表之后,每个栅格将被采样length(numberv)次(在这种情况下为4次).因为我有三个栅格,所以我想获得一个列表,其中第一个元素包括从我的三个栅格中分别采样100次,第二个栅格值分别采样150次等的采样值,等等.该列表将包含length(numberv)个元素.然后,我将使用这些位置来获取这些位置中的栅格值.

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.

我只用一个样本(使用了1个元素向量number)粘贴了一个更简单的案例的干净代码,希望对您有所帮助.

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

推荐答案

这项工作有效吗?

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

这篇关于在栅格列表清单上执行循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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