You can use the calc function too, based on the same example Roman gave

library(raster)
set.seed(1)
r1 <- raster(matrix(runif(9), ncol = 3))
r2 <- raster(matrix(runif(9), ncol = 3))
r3 <- raster(matrix(runif(9), ncol = 3))
r <- stack(r1, r2, r3)

res1 <- calc(r, fun = max) ## maximum value of all 3 rasters
res2 <- calc(r, fun = function(x) which.max(x)) ### number of the layer
with maximum value







On Sat, Mar 8, 2014 at 2:01 PM, Roman Luštrik <roman.lustrik@gmail.com>wrote:

> You mean something like this? Basically I just wrapped a function around
> extract.
>
> library(raster)
>
> r1 <- raster(matrix(runif(9), ncol = 3))
> r2 <- raster(matrix(runif(9), ncol = 3))
> r3 <- raster(matrix(runif(9), ncol = 3))
> r <- stack(r1, r2, r3)
>
> cellvals <- extract(r, 3)
> which(cellvals == max(cellvals))
>
> getStats <- function(cell, x, fun) {
>   getvals <- extract(x, cell)
>   ind <- which(getvals == fun(getvals))
>   getvals[, ind, drop = FALSE]
> }
>
> sapply(1:3, getStats, fun = max, x = r)
>
> Cheers,
> Roman
>
>
>
> On Sat, Mar 8, 2014 at 2:21 PM, Maurizio Marchi
> <mauriziomarchi85@gmail.com>wrote:
>
> > Hallo everybody.
> > Is there a way to see in a rasterstack with the raster package which
> layer
> > has the higer value for a selected pixel?
> > I have a rasterstack with 22 raster layers and I would like to have an
> > unique raster with, for each pixel, the maximum value of 22 rasters.
> > For example:
> > Pixel number 12345 , maximum value is 9876 from layer number XX.
> >
> > I hope to be clear enough...
> > Bests,
> > Maurizio
> >
> >
> > --
> > Maurizio Marchi
> > ID skype: maurizioxyz
> > *Ubuntu 12.04 LTS*
> > *"Il bello dell'open-source è che le domande possono essere poste alla
> > fonte"*
> > *utente linux 552.742*
> >
> >         [[alternative HTML version deleted]]
> >
> >
> > _______________________________________________
> > R-sig-Geo mailing list
> > R-sig-Geo@r-project.org
> > https://stat.ethz.ch/mailman/listinfo/r-sig-geo
> >
> >
>
>
> --
> In God we trust, all others bring data.
>
>         [[alternative HTML version deleted]]
>
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
>


-- 
Ahmadou H. DICKO
statistician economist (Ingénieur Statisticien Économiste)
PhD candidate in Climate change economics
Faculty of economics and managment - Cheikh Anta Diop University
West African Science Service Center on Climate Change and Adaptated Land
Use (WASCAL)
Center for Development Research (ZEF) - University of Bonn
email : ahmadou.dicko@ucad.edu.sn
twitter : @dickoah
github : github/dickoa <https://github.com/dickoa>
tel : +221 33 827 55 16
portable: +221 77 123 81 69

	[[alternative HTML version deleted]]

