Iris
Iris

Reputation: 1162

R: import two or more selected bands from an image stack

How can I import two or more selected bands of an image stack with R?

For reproducibility: I want to import band 1 and band 3 from image f.

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

I tried:

r <- stack(f, bands=c(1,3)) 
r <- stack(f, layers=c(1,3)) 
r <- stack(f, layers=c(f[1],f[3]))
r <- raster(f, band=c(1,3)) 

but I only got different error messages and no stack r with 2 bands.

Upvotes: 1

Views: 1242

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47156

I have fixed this problem in version 2.4-21 (perhaps available on R-Forge via install.packages("raster", repos="http://R-Forge.R-project.org") )

Upvotes: 1

jlhoward
jlhoward

Reputation: 59355

This seems to be what you're looking for.

plot(stack(f)[[c(1,3)]])

Upvotes: 1

Related Questions