Reputation: 865
Is there an easy way to load a raster directly into R as a matrix, instead of loading the raster, then using as.matrix()
to transform it into a matrix, i.e.
myras <- raster("file.tif")
mymat <- as.matrix(myras)
Upvotes: 2
Views: 1880
Reputation: 10340
Another possible solution would be to use as
like this:
library(raster)
mymat <- as(raster("file.tif"), "matrix")
Keep in mind, that reading a matrix directly from a file is not always an option. Since you are using a tiff file, you might have a compressed raster (e.g. by LZW, Packbits, etc.). Thus it is necessary to load and uncompress the raster first, before accessing the raster values and transform them into a matrix
.
Upvotes: 2
Reputation: 47036
There are similar alternatives, but I do not think there is an easier way. (except perhaps for png and some other graphics formats?). Without further explanation, it seems an odd question, as what you show is very concise. You can combine your two statements into one line (adding 8 characters to as.matrix)
library(raster)
myras <- as.matrix(raster("file.tif"))
Upvotes: 1