I'm not sure why this is the case, but the general rule appears to be that raster will convert integer values to numeric values whenever the indices themselves are of class numeric. To ensure that the returned value isn't converted to "numeric"
, you'll need to take care to only use indices of class "integer"
.
library(raster)
# Create an empty raster
r <- raster(nrow=10,ncol=10)
# fill it with integer values
r[] <- 1:100
class(1)
# [1] "numeric"
class(r[1])
# [1] "numeric"
class(1L)
# [1] "integer"
class(r[1L])
# [1] "integer"
class(1:2)
# [1] "integer"
class(r[1:2])
# [1] "integer"
class(1.7)
# [1] "numeric"
class(r[1.7])
# [1] "numeric"
class(r[as.integer(1.7)])
# [1] "integer"
class(r[as.integer(1.7)])
# [1] "integer"