4

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?

1 Answer 1

7

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) )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.