Lore_Bernicchi
Lore_Bernicchi

Reputation: 13

Converting a spatRaster to a RasterStack

I am working with a spatRaster of the 19 Bioclimatic variables of the WorldClim. I have to transform it to a RasterStack to check for VIF.

I already tried the raster() function, but it takes me only the first of the 19 rasters.

Thanks for your help!

Upvotes: 1

Views: 3650

Answers (1)

Yuri Gelsleichter
Yuri Gelsleichter

Reputation: 408

The function raster() calls only one layer (the first one) while stack() and brick() can call several layers.

The options are:

  1. raster::raster() to call the first layer only (band);

  2. raster::stack() to call all layers (less RAM needed);

  3. raster::brick() to call all layers (more efficient, and more RAM needed);

See the raster::brick() exemple:

library(raster)
b <- brick(system.file("external/rlogo.grd", package="raster"))
b
nlayers(b)
names(b)
plot(b)

Alternatively, you can try to write the object on disk and call again (same function) raster::stack() or raster::stack().

raster::stack():

  • Each layer in the RasterStack is loaded and processed independently, which can be more memory-efficient (occupies less RAM memory) when working with a large number of layers or very large layers.
  • Ideal for situations where individual layer manipulation is necessary or when layers may not have the same resolution or extent.

raster::brick():

  • It is more efficient in terms of performance and memory usage than a raster::stack(), as the layers are stored in a single array, which facilitates reading and writing to disk, being faster and more efficient in processing, however, it occupies more RAM memory.
  • Requires that all layers have the same resolution and extent. If this is not the case, the function may fail or automatically adjust the layers to match, which may not be desirable.

Upvotes: 2

Related Questions