[R-sig-Geo] raster: stackApply problems..

Robert J. Hijmans r.hijmans at gmail.com
Sat Dec 12 20:55:52 CET 2015


Mark,

Thanks for that clear report. This unexpected order of the layers
returned by stackApply is due to a bug which only reveals itself if
"unique(indices)"  is not sorted from 1 to n.
This has been fixed in version 2.5-1 (you can now also use a factor
variable, but that should not matter). This version should be
available here soon: https://r-forge.r-project.org/R/?group_id=294

Robert


On Fri, Dec 11, 2015 at 12:03 AM, Mark R Payne
<markpayneatwork at gmail.com> wrote:
> Hi,
>
> [reposted from r-help]
>
> 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
> problem:
>
> 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 head
> - 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,
>
> Mark
>
>         [[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