cells
. This is a hybrid function that uses
matrix for small numbers of loci (adj.raw(x = NULL, cells, directions = 8, sort = FALSE, pairs = TRUE,
include = FALSE, target = NULL, numCol = NULL, numCell = NULL,
match.adjacent = FALSE, cutoff.for.data.table = 10000, torus = FALSE)
adj(x = NULL, cells, directions = 8, sort = FALSE, pairs = TRUE,
include = FALSE, target = NULL, numCol = NULL, numCell = NULL,
match.adjacent = FALSE, cutoff.for.data.table = 10000, torus = FALSE)
match.adjacent
is TRUE.adjacent
function in the raster package.adjacent
in raster package. There is some extra
speed gain if NumCol and NumCells are passed rather than a raster.
Efficiency gains come from:
1. use data.table internally
- no need to remove NAs because wrapped or outside points are
just removed directly with data.table
- use data.table to sort and fast select (though not fastest possible)
2. don't make intermediate objects; just put calculation into return statement
The steps used in the algorithm are:
1. Calculate indices of neighbouring cells
2. Remove "to" cells that are
- <1 or="">numCells (i.e., they are above or below raster), using a single modulo calculation
- where the modulo of "to" cells is equal to 1 if "from" cells are 0 (wrapped right to left)
- or where the modulo of the "to" cells is equal to 0 if "from" cells are 1 (wrapped left to right)1>adjacent
library(raster)
a <- raster(extent(0,1000,0,1000),res=1)
sam <- sample(1:length(a),1e4)
numCol <- ncol(a)
numCell <- ncell(a)
adj.new <- adj(numCol=numCol,numCell=numCell,cells=sam,directions=8)
adj.new <- adj(numCol=numCol,numCell=numCell,cells=sam,directions=8,
include=TRUE)
if (interactive()) print(head(adj.new))
Run the code above in your browser using DataLab