Closed SteveCumming closed 6 years ago
Try raster function focal
, which does this.
I do not know of a calc that works on indices.
nope. focal does not meet the use case, because a vector-valued function is required.
I am not sure what you mean? Are you are referring to the argument, fun
? i.e., do you want "all the values in the adjacent cells" rather than "some summary of all the values"?
Incidentally, so we are clear, is this a reproducible example for what you are trying to do? But you just want more efficient?
aa <- adj(rast, 1:ncell(rast));
rast[][aa[,"to"]]
yes, I believe your reproducible example is what I meant. Maybe it's not what I need though. The raster is ratified. I need to count the frequency of each factor level in the neighbourhood of each cell, with the 0s in the right places. I'm having a little trouble, but it's not really a SpaDES problem.
rast <- raster::raster(extent(0,10,0,10), vals = sample(1:5, size = 100, replace = TRUE), res = 1);
aa <- adj(rast, 1:ncell(rast))
bb <- data.table::data.table(aa, value=rast[][aa[,"to"]], key = "from");
cc <- bb[,.N,by="from,value"]
cc
gives you the frequency of each factor level, from each "from" cell
So there are circumstances, as in implementing spatial dependencies in State and Transition Models, where one wants to know the values in the cells adjacent to a focal cell, not the indices. When this is needed for every cell in a large raster, it might be more efficient to have adj() return the array or dt of values, rather that to return the indices, and then to have the user extract the values.
Parenthetically, is there no analogue of calc that works on indices rather than values?