Thank you very much Andrew.

Instead of representing the LayerValue as a plot, I tried to use the
which.max() but end up with an error message
Error in which.max(s) : no method for coercing this S4 class to a vector

Even the example provided produce the same error.

Could anybody kindly test this?
b <- brick(system.file("external/rlogo.grd", package="raster"))
r <- which.min(b)
i <- which.min(b[[3]])
xy <- xyFromCell(b, i)
plot(b[[3]])
points(xy)




On Thu, Oct 3, 2013 at 6:14 PM, Andrew Vitale <vitale232@gmail.com> wrote:

> Eddie,
>
> raster:::calc is good for cell by cell computations.  There are some good
> examples on the help page.
>
> I made an example that produces a 2 layer raster stack, with layer 1 as
> the max values, layer 2 as the layer number from the original stack.
>
> -Andrew
>
> library(raster)
>
> r <- raster(nrow = 10, ncol = 10)
> s <- list()
> for (i in 1:12) {
>         s[[i]] <- setValues(r, rnorm(ncell(r), 10, 3) )
> }
>
> s <- stack(s)
>
> fun1 <- function(x){
>         m <- max(x)
>         w <- which(max(x) == x)
>         return(c(m, w))
>         }
>
> s2 <- calc(s, fun = fun1)
>
> plot(s2, main = c('Max', 'LayerValue'))
>
>
>
> On Thu, Oct 3, 2013 at 9:55 AM, Eddie Smith <eddieatr@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@r-project.org
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>
>
>
> --
> *Andrew P. Vitale*
> Masters Student
> Department of Geography
> University of Nevada, Reno
>
> vitale232@gmail.com
>

	[[alternative HTML version deleted]]

