[R] Raster-package - problem with stackApply()

MacQueen, Don macqueen1 at llnl.gov
Thu Dec 10 20:46:47 CET 2015

Appears to me that results for the third set of indices you supplied (1,1)
ended up in the third layer of the result. Similarly for the other sets of
indices. This makes sense to me.

r-sig-geo might be a better place to ask questions about the raster
package. I've seen other questions about stackApply there.


Don MacQueen

Lawrence Livermore National Laboratory
7000 East Ave., L-627
Livermore, CA 94550

On 12/10/15, 8:38 AM, "R-help on behalf of Mark R Payne"
<r-help-bounces at r-project.org on behalf of markpayneatwork at gmail.com>

>I am trying to use stackApply() to perform averages over subsets of a
>brick. However, I am struggling with the indices argument, and how it
>should be interpreted. Here is a simple working example illustrating my
>r <- raster()
>r[] <- 1
>inp <- brick(r,r,r,r,r,r)*(1:6)
>res <- stackApply(inp,c(2,2,3,3,1,1),mean)
>Now if we look at the values of each object:
>> inp
>names       : layer.1, layer.2, layer.3, layer.4, layer.5, layer.6
>min values  :       1,       2,       3,       4,       5,       6
>max values  :       1,       2,       3,       4,       5,       6
>> res
>names       : layer.1, layer.2, layer.3
>min values  :     3.5,     5.5,     1.5
>max values  :     3.5,     5.5,     1.5
>Now, the problem is that the names and order of the layers in "res" don't
>line up with the indices that I provided. You can do the maths in your
>- e.g. the first two layers of "inp" have values of 1 and 2, so their mean
>should be 1.5 - however, this is ending up as layer 3 in "res".
>So how should the indices argument be interpreted in this context?
>Suggestion: A more intuitive format for the "indices" argument in
>stackApply might be as a factor - this way the order is implict and
>stackApply ends up working similar to split() or tapply()...
>Best wishes,
>	[[alternative HTML version deleted]]
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>PLEASE do read the posting guide
>and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list