Karim Ghariani
Karim Ghariani

Reputation: 67

combine one band rasters to get a multiband raster

I have 4 one-band's rasters (same resolution, same extents...) that I want to combine to have a four-band's raster. Anyone could tell me how to do this with R? Thank you.

Upvotes: 3

Views: 5075

Answers (1)

Lucas Fortini
Lucas Fortini

Reputation: 2460

To create a raster stack of rasters of same extent and resolution, use the stack function in the raster package:

s  <-  stack(raster1,  raster2)

In this case the raster can be a raster object or a file path to a raster.

edit (concrete example):

library(raster)
fn  <-  system.file("external/test.grd",  package="raster")
s  <-  stack(raster(fn),  raster(fn)*2) #here i had to create the raster object since I 
#was multiplying one of the input rasters 
s

class       : RasterStack 
dimensions  : 115, 80, 9200, 2  (nrow, ncol, ncell, nlayers)
resolution  : 40, 40  (x, y)
extent      : 178400, 181600, 329400, 334000  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:28992     
+towgs84=565.237,50.0087,465.658,-0.406857,0.350733,-1.87035,4.0812 +proj=sterea 
+lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 
+ellps=bessel +units=m +no_defs 
names       :  test.1,  test.2 
min values  : 128.434, 256.868 
max values  : 1805.78, 3611.56 

Upvotes: 3

Related Questions