Note that the documentation for get.checkerboard indicates occ and bg can be either a matrix or data.frame.
Steps to reproduce error:
Create data, as per help page:
library(terra)
set.seed(1)
### Create environmental extent (raster)
envs <- rast(nrow = 25, ncol = 25, xmin = 0, xmax = 1, ymin = 0, ymax = 1)
### Create occurrence localities
set.seed(1)
nocc <- 25
xocc <- rnorm(nocc, sd=0.25) + 0.5
yocc <- runif(nocc, 0, 1)
occs <- as.data.frame(cbind(xocc, yocc))
### Create background points
nbg <- 500
xbg <- runif(nbg, 0, 1)
ybg <- runif(nbg, 0, 1)
bg <- as.data.frame(cbind(xbg, ybg))
This works as designed for data.frames:
get.checkerboard(occs, envs, bg, aggregation.factor = 4)
Generating basic checkerboard partitions...
$occs.grp
[1] 2 2 1 2 1 1 2 2 1 2 2 2 2 1 2 1 2 2 1 1 2 2 1 2
$bg.grp
[1] 2 2 2 1 2 1 1 1 1 1 1 2 1 2 2 2 1 2 2 2 1 2 2 1 1 2 1 1 2 2 2 1 2 2 1 1 1 1 1 2 2 2 1 1 2 1 2 2 1 1 1 2 2 1
[55] 2 2 2 1 2 1 2 1 1 2 2 1 2 2 2 2 1 2 1 2 2 2 2 2 2 2 1 1 1 1 2 1 1 2 1 2 1 1 2 2 1 1 2 1 1 2 1 2 2 2 2 1 1 1
[109] 1 2 1 2 2 2 2 2 2 2 2 1 2 2 1 2 2 2 2 1 2 2 1 1 1 2 2 2 2 2 1 1 1 1 1 1 2 2 2 1 1 2 2 2 1 2 1 2 1 2 1 2 1 1
[163] 1 2 2 2 2 2 1 1 1 2 2 2 2 2 2 1 1 1 2 1 2 1 1 1 1 2 1 2 1 1 2 2 1 2 2 1 2 2 1 2 1 1 1 1 1 1 2 1 2 1 1 2 1 1
[217] 2 1 1 1 1 1 2 2 2 1 1 2 2 1 1 2 2 1 1 1 1 2 1 1 2 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 1 1 2 1 2 1 2 2 1 1 2 2 2
[271] 1 1 2 2 1 2 2 2 1 1 2 2 2 2 1 1 1 1 2 1 1 2 1 2 2 2 2 1 2 1 1 1 1 1 2 1 2 1 1 1 1 2 2 1 1 1 2 1 2 1 2 1 2 1
[325] 2 2 1 2 1 2 2 2 2 2 1 1 2 2 2 1 2 1 1 2 2 1 2 1 1 1 2 2 2 1 1 2 1 1 2 2 1 1 2 2 2 1 2 1 1 1 2 1 1 1 2 2 2 1
[379] 2 1 1 2 2 1 1 1 2 2 1 2 2 1 2 1 2 1 2 2 1 2 1 2 1 1 2 2 2 2 1 1 1 2 1 2 1 2 1 2 2 2 2 1 2 1 1 2 2 2 1 2 1 2
[433] 2 2 2 2 2 2 1 2 2 1 2 2 2 2 2 2 1 2 1 1 1 2 2 2 2 1 2 1 2 1 2 2 1 1 1 1 2 1 2 1 1 1 1 1 2 2 2 1 2 2 2 2 1 2
[487] 1 1 2 2 1 2 2 2 1 1 1 2 2 2
However, if we supply the occurrences and background as matrices instead:
get.checkerboard(as.matrix(occs), envs, as.matrix(bg), aggregation.factor = 4)
Generating basic checkerboard partitions...
Error in fix.by(by.y, y) : 'by' must specify uniquely valid columns
This is likely a very rare situation, I only encountered it when trying to debug some other issues. The documentation suggests occ and bg can be matrices or data.frames, and this is true when using ENMevaluate, as this function explicitly converts matrices to data.frames:
|
occs <- as.data.frame(occs) |
|
if(!is.null(bg)) bg <- as.data.frame(bg) |
The problem is caused in the function get.checkerboard, which calls the names function on objects derived from occs and bg in a few places, e.g.:
|
names(occs.rn) <- c(names(occs.r)[1:2], "row") |
names() refers to column names for data.frames, but not in matrices.
I think this could be fixed by switching the calls to names to colnames in get.checkerboard, or by explicitly coercing occs and bg to data.frames in get.checkerboard, as is already done in ENMevaluate.
Note that the documentation for
get.checkerboardindicatesoccandbgcan be either a matrix or data.frame.Steps to reproduce error:
Create data, as per help page:
This works as designed for data.frames:
However, if we supply the occurrences and background as matrices instead:
This is likely a very rare situation, I only encountered it when trying to debug some other issues. The documentation suggests
occandbgcan be matrices or data.frames, and this is true when usingENMevaluate, as this function explicitly converts matrices to data.frames:ENMeval/R/ENMevaluate.R
Lines 326 to 327 in bbfaf8d
The problem is caused in the function
get.checkerboard, which calls thenamesfunction on objects derived fromoccsandbgin a few places, e.g.:ENMeval/R/partitions.R
Line 301 in bbfaf8d
names()refers to column names for data.frames, but not in matrices.I think this could be fixed by switching the calls to
namestocolnamesinget.checkerboard, or by explicitly coercingoccsandbgto data.frames inget.checkerboard, as is already done inENMevaluate.