ropsi
ropsi

Reputation: 141

R clip raster with multiple bands

I want to create a subset of an image with four bands. Therefore I am using the crop function in R.

A<-raster("L8_stacked.tif")
subset<-extent(c(639451, 660104, 5469254, 5489566))
B<-crop(A,subset)

As a result I get a raster with only one band in the .tif file. Do I have to define other options to get a subset image with 4 bands?

Upvotes: 6

Views: 3248

Answers (1)

maRtin
maRtin

Reputation: 6516

As the others already pointed out in the comments, the raster() function returns a (single) RasterLayer object. If you want a multilayer raster object you need to use the stack() or brick() function load the image into R. I.e.:

A <- stack("L8_stacked.tif")

If you then apply your extent with the crop() function, the result should be a raster stack containing all the bands from the original image.

To learn more on the raster package, read this document.

Upvotes: 11

Related Questions