Pregunta

In part because I want to limit memory usage, I want to create a raster with integer values. I'm using the package raster in R. Although I appear to be able to create a raster with integer values, the extracted values are numeric. Here is a simple example.

library(raster)
# Create an empty raster
r <- raster(nrow=10,ncol=10)
# fill it with integer values
r[] <- 1:100
# It look like it's a integer raster
class(r[])
# but when you extract a value it's a numeric
class(r[1])
# if you extract it this way you get an integer
class(r[][1])
# But that's not always practical

Would someone be able to explain why this is the case and how I can get a raster that returns an integer value when I use r[1]?

I could not find anything regarding integer values, I found a post that discuss categorical variables. But categorical variables take more memory than integer see:

object.size(r)
object.size(ratify(r))

Many thanks! Marie

¿Fue útil?

Solución

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"

Otros consejos

In general, you should use getValues(), which will return the correct class. In your case:

class(getValues(r))

Now to understand what is happening with [], look at the source code in index.R:

  1. setMethod("[", c("Raster", "missing", "missing") uses getValues() internally, which returns correct result.

  2. setMethod("[", c("Raster", "numeric", "missing"),: uses raster:::.doExtract internally, which calls raster:::.readCells(). That last function contains a call to cbind(uniquecells, vals), creating a matrix. A matrix in R can have only one type. Hence, even if vals is integer, uniquecells being numeric could make the whole numeric.

So when you used r[], you were in case 1, while when you do r[1], you are in case 2.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top