R Crop no-data of a raster

Wraf picture Wraf · Oct 30, 2013 · Viewed 8.3k times · Source

I would like to crop the no-data part of some rasters (example of the image in 1 where no-data is in black) without defining the extent manually.

Any idea?

image with no-data

Answer

Robert Hijmans picture Robert Hijmans · Dec 5, 2015

You can use trim for that:

library(raster)
r <- raster(ncol=18,nrow=18)
r[39:49] <- 1
r[205] <- 6
s <- trim(r)