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, id = NULL)
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, id = NULL)
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 statementThe 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)
Run the code above in your browser using DataLab