使用';计算';R中的函数

使用';计算';R中的函数,r,coordinates,raster,r-raster,calc,R,Coordinates,Raster,R Raster,Calc,我试图在R中运行一个“calc”函数,但似乎找不到一种方法来获取正在处理的单元格的坐标。我想做的很简单: 在二进制光栅(0和1)上使用“calc”函数--如果光栅值为“0”,则更改为“NA”。如果光栅值为“1”,则应用一系列过程,我需要将单元格坐标存储到变量中 processAllCells = function(cell) { if (cell == 0) { cell = NA return(cell) } else { cellCoords = coo

我试图在R中运行一个“calc”函数,但似乎找不到一种方法来获取正在处理的单元格的坐标。我想做的很简单: 在二进制光栅(0和1)上使用“calc”函数--如果光栅值为“0”,则更改为“NA”。如果光栅值为“1”,则应用一系列过程,我需要将单元格坐标存储到变量中

processAllCells = function(cell) {
  if (cell == 0) {
    cell = NA
    return(cell) 
  }
  else {
    cellCoords = coordinates(cell) ### This is what I'm trying to do. This does not work. See the error message.
    ### Here will go further processes using the cell coordinates.
    return(cell)
  }
}

outputRaster = calc(lake, processAllCells)
错误消息:

Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘coordinates’ for signature ‘"integer"’
In addition: Warning message:
In if (cell == 0) { :

 Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘coordinates’ for signature ‘"integer"’ 

谢谢大家

这是不可能的。但您可以使用x和y坐标制作光栅层,并在
calc
中使用它们

library(raster)
r <- raster(nrow=10, ncol=10, values=1:100)
x <- init(r, "x")
y <- init(r, "y")
库(光栅)

r您是从某个库导入
坐标
函数还是自己编写的?它是从“光栅”包中定义的函数。该函数使用对象来计算坐标,而不是整数或整数向量。这可能就是你遇到的问题。是的,这正是问题所在。变量“cell”仅包含其值,因此“1”。但我想知道是否有人知道如何得到它的坐标。
s <- stack(r, x, y)
#x <- calc(s, your-function)