[GIS] How to extract single band from multi-band radiometry raster using R

envirrasterrgdal

I am having trouble extracting single band from multiple multi-band raster. I have no problem doing it on single raster file.

Is there a way to loop through all the raster images to extract a single band in R?

Best Answer

It is not clear on weather you want to subset bands upon reading into R or extract a single band from an existing raster stack. Once illustrated, both are quite simple.

We can use the 3 band R logo as an example.

library(raster)
fn <- system.file("external/rlogo.grd", package="raster")

To subset a band from an R raster stack/brick you use a double bracket, like you are indexing a list object.

( r <- stack(fn) )  
( r1 <- r[[1]] )

To read a single band from a multiband raster, on disk, you use the "raster" function with the "band" argument.

( r <- raster(fn, band = 1) )