我的问题是如何计算 R 中两个栅格之间的角度?这是一个可复制的示例和我得到的错误:
angle <-function(vectora, vectorb){
theta <- acos( sum(vectora*vectorb) / ( sqrt(sum(vectora * vectora)) * sqrt(sum(vectorb * vectorb)) ))
}
raster1 <- raster(ncols=150, nrows=150, xmn=0)
raster1[] <- rnorm(150*150,ncell(1))
raster2 <- raster(ncols=150, nrows=150, xmn=0)
raster2[] <- rnorm(150*150,ncell(2))
r.angle <- overlay(raster1,raster2, filename='tes.tif',fun=function(x,y){angle(x,y)})
错误消息如下:
Error in (function (x, fun, filename = "", recycle = TRUE, ...) :
cannot use this formula, probably because it is not vectorized
我怎样才能得到那个矢量化?