Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于焦点统计(焦点,“光栅”包,R)的自身函数提供不正确的输出_R_Geospatial_Raster - Fatal编程技术网

用于焦点统计(焦点,“光栅”包,R)的自身函数提供不正确的输出

用于焦点统计(焦点,“光栅”包,R)的自身函数提供不正确的输出,r,geospatial,raster,R,Geospatial,Raster,我是R和编程的初学者。所以我认为问题很简单,但我无法找到答案或解决它 我有光栅(100*100个单元格)。我需要在移动窗口(例如,窗口大小=21)通过2D DFT获得谐波幅值的中值。 我在光栅包中找到了焦距函数。对于这个函数,我可以编写自己的函数,该函数采用系列值(窗口中的光栅值)并返回整个窗口的单个值 r <- raster(matrix(rnorm(10000), nrow = 100, ncol = 100)) # creation of raster win <- 21 #

我是R和编程的初学者。所以我认为问题很简单,但我无法找到答案或解决它

我有光栅(100*100个单元格)。我需要在移动窗口(例如,窗口大小=21)通过2D DFT获得谐波幅值的中值。 我在光栅包中找到了焦距函数。对于这个函数,我可以编写自己的函数,该函数采用系列值(窗口中的光栅值)并返回整个窗口的单个值

r <- raster(matrix(rnorm(10000), nrow = 100, ncol = 100)) # creation of raster
win <- 21 # setting the window size
spectr <- function(d) {
  return(median(abs(spec.fft(x = 1:win, y = 1:win, z = (d - mean(d)))$A)))
} # i think "d" - the matrix of raster values in the window border
focal(x = r, w = matrix(1, win, win), fun = spectr())

首先,要使用前面在
focal()
中定义的函数,只需删除函数名后的括号
()

其次,使用
spectral::spec.fft
函数,它要求
z
参数为矩阵。然而,正如我们从
?focal
中学到的,focal转发了一个值向量:

fun函数应该包含多个数字,并返回一个数字

因此,您必须自己生成所需的矩阵

参见此示例(但是,请检查输出的有效性):


谢谢你,洛基!这真的很简单。但我认为函数“spectr”中的错误。您的代码正常工作)
install.packages("spectral")
library(spectral)
spectr <- function(d) {
  return(median(abs(spec.fft(x = 1:win, y = 1:win, 
                             z = (matrix(d, ncol = win, nrow = win) - mean(d)))$A
                             # eventually you have to reorder or transpose the matrix 
                             # if its order has some impact on spec.fft
                    )))
} 
focal(x = r, w = matrix(1, win, win), fun = spectr)
# class       : RasterLayer 
# dimensions  : 100, 100, 10000  (nrow, ncol, ncell)
# resolution  : 0.01, 0.01  (x, y)
# extent      : 0, 1, 0, 1  (xmin, xmax, ymin, ymax)
# coord. ref. : NA 
# data source : in memory
# names       : layer 
# values      : 0.03341064, 0.04557778  (min, max)