简体   繁体   English

为R中的RasterBrick创建列表列表循环

[英]Create a loop for a list of list from RasterBrick in R

I'm on a project in remote sensing running on R. I've got a RasterBrick(x) with the raster for all the dates I'm interested in, a Time Serie with the dates corresponding (called time in the function), and a function which works as I want it when processed manually (z is the pixel I want) : 我正在一个在R上运行的遥感项目中。我有一个RasterBrick(x),其中包含我感兴趣的所有日期的栅格,带有相应日期的Time Serie(在函数中称为时间),和一个功能,当我手动处理时可以按我的需要工作(z是我想要的像素):

function(x,z)
{
d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
l[[z]]<-list(n$output[[1]]$Tt)
}

The bfastts function is used to create a ts object containing the values of one pixel along the time serie, the bfast is another processing some statisticals of which I only want one result (this is the third line)? bfastts函数用于创建一个ts对象,该对象包含沿时间序列的一个像素的值,bfast是另一种处理某些统计信息的统计,我只需要一个结果(这是第三行)? None of this two functions are mine, and they are stable and foundable in the R package repository. 这两个函数都不是我的,它们在R软件包存储库中是稳定且可发现的。

So, I would like to add "another level" of function (sorry for my vocabulary which may not be very precise) which would allow to run this function automatically. 因此,我想添加功能的“另一个级别”(抱歉,我的词汇可能不太准确),这将允许自动运行此功能。 My expected result would be a list of the result of the function above, so in other words a list of each pixel's time series. 我的预期结果是上面函数结果的列表,换句话说就是每个像素时间序列的列表。

I've tried this (x is still the RasterBrick) : 我已经试过了(x仍然是RasterBrick):

function(x)
{
  z<-nrow(x)*ncol(x)
  j<-last(z[[1]])
  l<-vector('list',length = j)
  index<-function(x)
    {
    d<-bfastts(as.vector(x[as.numeric(z)]),time,type="16-day")
    n<-bfast(d, h=0.15, season="harmonic", max.iter = 1)
    l[[z]]<-list(n$output[[1]]$Tt) # this is to add the newly created element to the list
  }
  lapply(x, FUN='index')
}

but I'm getting an answer that it is not possible to coerce a S4 object to a vector, I guess the problem is in lapply who doesn't like the RasterBrick class... Furthermore I want a list of list in output, and not a list of RasterBrick (I think I understood lapply returns a list of object with the same class as x). 但是我得到的答案是不可能将S4对象强制转换为矢量,我想问题出在不喜欢RasterBrick类的lapply上。此外,我想要输出中的列表列表,并且不是RasterBrick的列表(我想我理解lapply返回的对象列表与x相同)。

I've tried different workaround, none succesfully, which is not surprising giving my low level in programming, and this one seems to me the closest to what I need. 我尝试了不同的解决方法,但都没有成功,这不足为奇,因为我的编程水平很低,而在我看来,这是最接近我需要的方法。 I don't think I fully understand neither how lapply works nor the use of a function in a function. 我想我既不完全了解lapply的工作原理,也不完全理解函数中函数的用法。

Thank you very much if you can help me. 如果可以帮助我,非常感谢。 Cheers Guillaume 干杯纪尧姆

因此,以防万一它对某人有用,这是我解决此问题的方法(最终看起来很简单),“砖”对象是RasterBrick:

pixelts<- as.list(as.data.frame(t(as.data.frame(brick))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM