[R-sig-Geo] Creating a maximum raster layer from RasterStack

Agustin Lobo alobolistas at gmail.com
Mon Oct 7 20:18:45 CEST 2013


What about this:
r1 <- raster(matrix(rnorm(100,5,10),10,10))
r2 <- raster(matrix(rnorm(100,2,30),10,10))
s <- stack(r1,r2)
mx <- stackApply(s,max)

which.max() fails because raster requires the na.rm argument (I think, Robert?)

mxd <- stackApply(s,indices=c(1,1),which.max)
Error in FUN(newX[, i], ...) : unused argument (na.rm = TRUE)

so make your own function icluding the na.rm argument:

miwhichmax <- function(x,na.rm=TRUE) which.max(x)
mxd <- stackApply(s,indices=c(1,1),miwhichmax)

Agus


On Thu, Oct 3, 2013 at 6:55 PM, Eddie Smith <eddieatr at gmail.com> wrote:
> Dear list,
>
> I have a RasterLayer that contains 365 bands with a dimension of 1032,
> 1656, 1708992  (nrow, ncol, ncell).
>
> library(raster)
> img <- list.files(pattern='\\.img$')
> stack <- stack(img)
> data2002 <- writeRaster(stack, filename="2002.tif", format="GTiff",
> overwrite=TRUE)
>
> 1. How could I produce a raster layer that contains a maximum value from
> 365 bands for each pixel?
> I tried maxValue() but that only give me a maximum value for each band.
>
> 2. If I manage to do step 1, is it possible to know from which band is
> actually the maximum value originate?
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list