[R-sig-Geo] which.max() limit in raster package for RasterBrick

Robert J. Hijmans r.hijmans at gmail.com
Tue Sep 23 21:01:55 CEST 2014


Tyler,

It is probably useful to study the vignette that comes with the raster
package if you have not done so. Also please provide a reproducible
example, if possible, and report the error message you get. Presumably
it is:  "Error in which.min(harvest_area) : not yet implemented for
large objects". I will fix that.

In this case, I think you can do the following if there are no cells
with NA for all layers:

x <- calc(harvest_area, which.max)

else you can do:

x = calc(b, function(i){ v <- which.max(i); ifelse(length(v) == 0, NA, v)})


By the way, it is not a good idea to write to slots "@" of Raster*
objects as you do. I think your first lines of code can be replaced
with

f <- list.files(pattern="*.nc$", recursive=TRUE)
harvest_area <- stack(f)
# or if specifying the band is necessary:
harvest_area <- stack(lapply(f, function(i) raster(i, band=1)))
names(harvest_area) <- sub("\\/.*", "", f)

Robert


2014-09-23 11:01 GMT-07:00 Tyler Frazier <tyler.j.frazier at icloud.com>:
> Dear group,
>
> I have a RasterBrick with 16 layers created with the stack command like so:
>
> f <- list.files(pattern="*.nc$", recursive=TRUE)
> r <- raster(f[1], band = 1)
> harvest_area <- stack(r)
> harvest_area at layers <- sapply(f, function(x) {r at file@name=x;r})
> names(harvest_area) <- sub("\\/.*", "", f)
>
> and then I want to apply which.max() to determine which layer is max for each grid cell.  It seems which.max() returns an error for a brick with more than 5? layers.
>
> I have attempted various approaches to determine the max of each grid cell within a RasterBrick with more than 5 layers, but the only thing I can get to work is a very verbose approach (following the msg).
>
> Various approaches using a defined function as such:
>
> con <- function(condition, trueValue, falseValue) {return(condition * trueValue + (!condition)*falseValue)}
>
> including:
>
> for (k in 1:length(harvest_area)) {test <- con(harvest_area[[k]] > harvest_area[[-k]], k, 0)}
>
> and:
>
> dom_ha <- con(harvest_area[[1]] > harvest_area[[-1]], 1,
>                   con(harvest_area[[2]] > harvest_area[[-2]], 2,
>                   con(harvest_area[[3]] > harvest_area[[-3]], 3,
>                   con(harvest_area[[4]] > harvest_area[[-4]], 4,
>                   con(harvest_area[[5]] > harvest_area[[-5]], 5,
>                   con(harvest_area[[6]] > harvest_area[[-6]], 6,
>                   con(harvest_area[[7]] > harvest_area[[-7]], 7,
>                   con(harvest_area[[8]] > harvest_area[[-8]], 8,
>                   con(harvest_area[[9]] > harvest_area[[-9]], 9,
>                   con(harvest_area[[10]] > harvest_area[[-10]], 10,
>                   con(harvest_area[[11]] > harvest_area[[-11]], 11,
>                   con(harvest_area[[12]] > harvest_area[[-12]], 12,
>                   con(harvest_area[[13]] > harvest_area[[-13]], 13,
>                   con(harvest_area[[14]] > harvest_area[[-14]], 14,
>                   con(harvest_area[[15]] > harvest_area[[-15]], 15,
>                   con(harvest_area[[16]] > harvest_area[[-16]], 16, 0
>                    ))))))))))))))))
>
> didn’t work.  I also tried using stackApply() with function(x) but it returned an error similar to the which.max() > 5.  The only thing that work is the following, but its so verbose.
>
> Any suggestions will be much appreciated.
>
> Thank you,
> Tyler
>
> dom_ha <- con(harvest_area[[1]] > harvest_area[[2]] &
>            harvest_area[[1]] > harvest_area[[3]] &
>            harvest_area[[1]] > harvest_area[[4]] &
>            harvest_area[[1]] > harvest_area[[5]] &
>            harvest_area[[1]] > harvest_area[[6]] &
>            harvest_area[[1]] > harvest_area[[7]] &
>            harvest_area[[1]] > harvest_area[[8]] &
>            harvest_area[[1]] > harvest_area[[9]] &
>            harvest_area[[1]] > harvest_area[[10]] &
>            harvest_area[[1]] > harvest_area[[11]] &
>            harvest_area[[1]] > harvest_area[[12]] &
>            harvest_area[[1]] > harvest_area[[13]] &
>            harvest_area[[1]] > harvest_area[[14]] &
>            harvest_area[[1]] > harvest_area[[15]] &
>            harvest_area[[1]] > harvest_area[[16]], 1,
>
>        con(harvest_area[[2]] > harvest_area[[1]] &
>            harvest_area[[2]] > harvest_area[[3]] &
>            harvest_area[[2]] > harvest_area[[4]] &
>            harvest_area[[2]] > harvest_area[[5]] &
>            harvest_area[[2]] > harvest_area[[6]] &
>            harvest_area[[2]] > harvest_area[[7]] &
>            harvest_area[[2]] > harvest_area[[8]] &
>            harvest_area[[2]] > harvest_area[[9]] &
>            harvest_area[[2]] > harvest_area[[10]] &
>            harvest_area[[2]] > harvest_area[[11]] &
>            harvest_area[[2]] > harvest_area[[12]] &
>            harvest_area[[2]] > harvest_area[[13]] &
>            harvest_area[[2]] > harvest_area[[14]] &
>            harvest_area[[2]] > harvest_area[[15]] &
>            harvest_area[[2]] > harvest_area[[16]], 2,
>
>        con(harvest_area[[3]] > harvest_area[[1]] &
>            harvest_area[[3]] > harvest_area[[2]] &
>            harvest_area[[3]] > harvest_area[[4]] &
>            harvest_area[[3]] > harvest_area[[5]] &
>            harvest_area[[3]] > harvest_area[[6]] &
>            harvest_area[[3]] > harvest_area[[7]] &
>            harvest_area[[3]] > harvest_area[[8]] &
>            harvest_area[[3]] > harvest_area[[9]] &
>            harvest_area[[3]] > harvest_area[[10]] &
>            harvest_area[[3]] > harvest_area[[11]] &
>            harvest_area[[3]] > harvest_area[[12]] &
>            harvest_area[[3]] > harvest_area[[13]] &
>            harvest_area[[3]] > harvest_area[[14]] &
>            harvest_area[[3]] > harvest_area[[15]] &
>            harvest_area[[3]] > harvest_area[[16]], 3,
>
>        con(harvest_area[[4]] > harvest_area[[1]] &
>            harvest_area[[4]] > harvest_area[[2]] &
>            harvest_area[[4]] > harvest_area[[3]] &
>            harvest_area[[4]] > harvest_area[[5]] &
>            harvest_area[[4]] > harvest_area[[6]] &
>            harvest_area[[4]] > harvest_area[[7]] &
>            harvest_area[[4]] > harvest_area[[8]] &
>            harvest_area[[4]] > harvest_area[[9]] &
>            harvest_area[[4]] > harvest_area[[10]] &
>            harvest_area[[4]] > harvest_area[[11]] &
>            harvest_area[[4]] > harvest_area[[12]] &
>            harvest_area[[4]] > harvest_area[[13]] &
>            harvest_area[[4]] > harvest_area[[14]] &
>            harvest_area[[4]] > harvest_area[[15]] &
>            harvest_area[[4]] > harvest_area[[16]], 4,
>
>        con(harvest_area[[5]] > harvest_area[[1]] &
>            harvest_area[[5]] > harvest_area[[2]] &
>            harvest_area[[5]] > harvest_area[[3]] &
>            harvest_area[[5]] > harvest_area[[4]] &
>            harvest_area[[5]] > harvest_area[[6]] &
>            harvest_area[[5]] > harvest_area[[7]] &
>            harvest_area[[5]] > harvest_area[[8]] &
>            harvest_area[[5]] > harvest_area[[9]] &
>            harvest_area[[5]] > harvest_area[[10]] &
>            harvest_area[[5]] > harvest_area[[11]] &
>            harvest_area[[5]] > harvest_area[[12]] &
>            harvest_area[[5]] > harvest_area[[13]] &
>            harvest_area[[5]] > harvest_area[[14]] &
>            harvest_area[[5]] > harvest_area[[15]] &
>            harvest_area[[5]] > harvest_area[[16]], 5,
>
>        con(harvest_area[[6]] > harvest_area[[1]] &
>            harvest_area[[6]] > harvest_area[[2]] &
>            harvest_area[[6]] > harvest_area[[3]] &
>            harvest_area[[6]] > harvest_area[[4]] &
>            harvest_area[[6]] > harvest_area[[5]] &
>            harvest_area[[6]] > harvest_area[[7]] &
>            harvest_area[[6]] > harvest_area[[8]] &
>            harvest_area[[6]] > harvest_area[[9]] &
>            harvest_area[[6]] > harvest_area[[10]] &
>            harvest_area[[6]] > harvest_area[[11]] &
>            harvest_area[[6]] > harvest_area[[12]] &
>            harvest_area[[6]] > harvest_area[[13]] &
>            harvest_area[[6]] > harvest_area[[14]] &
>            harvest_area[[6]] > harvest_area[[15]] &
>            harvest_area[[6]] > harvest_area[[16]], 6,
>
>        con(harvest_area[[7]] > harvest_area[[1]] &
>            harvest_area[[7]] > harvest_area[[2]] &
>            harvest_area[[7]] > harvest_area[[3]] &
>            harvest_area[[7]] > harvest_area[[4]] &
>            harvest_area[[7]] > harvest_area[[5]] &
>            harvest_area[[7]] > harvest_area[[6]] &
>            harvest_area[[7]] > harvest_area[[8]] &
>            harvest_area[[7]] > harvest_area[[9]] &
>            harvest_area[[7]] > harvest_area[[10]] &
>            harvest_area[[7]] > harvest_area[[11]] &
>            harvest_area[[7]] > harvest_area[[12]] &
>            harvest_area[[7]] > harvest_area[[13]] &
>            harvest_area[[7]] > harvest_area[[14]] &
>            harvest_area[[7]] > harvest_area[[15]] &
>            harvest_area[[7]] > harvest_area[[16]], 7,
>
>        con(harvest_area[[8]] > harvest_area[[1]] &
>            harvest_area[[8]] > harvest_area[[2]] &
>            harvest_area[[8]] > harvest_area[[3]] &
>            harvest_area[[8]] > harvest_area[[4]] &
>            harvest_area[[8]] > harvest_area[[5]] &
>            harvest_area[[8]] > harvest_area[[6]] &
>            harvest_area[[8]] > harvest_area[[7]] &
>            harvest_area[[8]] > harvest_area[[9]] &
>            harvest_area[[8]] > harvest_area[[10]] &
>            harvest_area[[8]] > harvest_area[[11]] &
>            harvest_area[[8]] > harvest_area[[12]] &
>            harvest_area[[8]] > harvest_area[[13]] &
>            harvest_area[[8]] > harvest_area[[14]] &
>            harvest_area[[8]] > harvest_area[[15]] &
>            harvest_area[[8]] > harvest_area[[16]], 8,
>
>        con(harvest_area[[9]] > harvest_area[[1]] &
>            harvest_area[[9]] > harvest_area[[2]] &
>            harvest_area[[9]] > harvest_area[[3]] &
>            harvest_area[[9]] > harvest_area[[4]] &
>            harvest_area[[9]] > harvest_area[[5]] &
>            harvest_area[[9]] > harvest_area[[6]] &
>            harvest_area[[9]] > harvest_area[[7]] &
>            harvest_area[[9]] > harvest_area[[8]] &
>            harvest_area[[9]] > harvest_area[[10]] &
>            harvest_area[[9]] > harvest_area[[11]] &
>            harvest_area[[9]] > harvest_area[[12]] &
>            harvest_area[[9]] > harvest_area[[13]] &
>            harvest_area[[9]] > harvest_area[[14]] &
>            harvest_area[[9]] > harvest_area[[15]] &
>            harvest_area[[9]] > harvest_area[[16]], 9,
>
>        con(harvest_area[[10]] > harvest_area[[1]] &
>            harvest_area[[10]] > harvest_area[[2]] &
>            harvest_area[[10]] > harvest_area[[3]] &
>            harvest_area[[10]] > harvest_area[[4]] &
>            harvest_area[[10]] > harvest_area[[5]] &
>            harvest_area[[10]] > harvest_area[[6]] &
>            harvest_area[[10]] > harvest_area[[7]] &
>            harvest_area[[10]] > harvest_area[[8]] &
>            harvest_area[[10]] > harvest_area[[9]] &
>            harvest_area[[10]] > harvest_area[[11]] &
>            harvest_area[[10]] > harvest_area[[12]] &
>            harvest_area[[10]] > harvest_area[[13]] &
>            harvest_area[[10]] > harvest_area[[14]] &
>            harvest_area[[10]] > harvest_area[[15]] &
>            harvest_area[[10]] > harvest_area[[16]], 10,
>
>        con(harvest_area[[11]] > harvest_area[[1]] &
>            harvest_area[[11]] > harvest_area[[2]] &
>            harvest_area[[11]] > harvest_area[[3]] &
>            harvest_area[[11]] > harvest_area[[4]] &
>            harvest_area[[11]] > harvest_area[[5]] &
>            harvest_area[[11]] > harvest_area[[6]] &
>            harvest_area[[11]] > harvest_area[[7]] &
>            harvest_area[[11]] > harvest_area[[8]] &
>            harvest_area[[11]] > harvest_area[[9]] &
>            harvest_area[[11]] > harvest_area[[10]] &
>            harvest_area[[11]] > harvest_area[[12]] &
>            harvest_area[[11]] > harvest_area[[13]] &
>            harvest_area[[11]] > harvest_area[[14]] &
>            harvest_area[[11]] > harvest_area[[15]] &
>            harvest_area[[11]] > harvest_area[[16]], 11,
>
>        con(harvest_area[[12]] > harvest_area[[1]] &
>            harvest_area[[12]] > harvest_area[[2]] &
>            harvest_area[[12]] > harvest_area[[3]] &
>            harvest_area[[12]] > harvest_area[[4]] &
>            harvest_area[[12]] > harvest_area[[5]] &
>            harvest_area[[12]] > harvest_area[[6]] &
>            harvest_area[[12]] > harvest_area[[7]] &
>            harvest_area[[12]] > harvest_area[[8]] &
>            harvest_area[[12]] > harvest_area[[9]] &
>            harvest_area[[12]] > harvest_area[[10]] &
>            harvest_area[[12]] > harvest_area[[11]] &
>            harvest_area[[12]] > harvest_area[[13]] &
>            harvest_area[[12]] > harvest_area[[14]] &
>            harvest_area[[12]] > harvest_area[[15]] &
>            harvest_area[[12]] > harvest_area[[16]], 12,
>
>        con(harvest_area[[13]] > harvest_area[[1]] &
>            harvest_area[[13]] > harvest_area[[2]] &
>            harvest_area[[13]] > harvest_area[[3]] &
>            harvest_area[[13]] > harvest_area[[4]] &
>            harvest_area[[13]] > harvest_area[[5]] &
>            harvest_area[[13]] > harvest_area[[6]] &
>            harvest_area[[13]] > harvest_area[[7]] &
>            harvest_area[[13]] > harvest_area[[8]] &
>            harvest_area[[13]] > harvest_area[[9]] &
>            harvest_area[[13]] > harvest_area[[10]] &
>            harvest_area[[13]] > harvest_area[[11]] &
>            harvest_area[[13]] > harvest_area[[12]] &
>            harvest_area[[13]] > harvest_area[[14]] &
>            harvest_area[[13]] > harvest_area[[15]] &
>            harvest_area[[13]] > harvest_area[[16]], 13,
>
>        con(harvest_area[[14]] > harvest_area[[1]] &
>            harvest_area[[14]] > harvest_area[[2]] &
>            harvest_area[[14]] > harvest_area[[3]] &
>            harvest_area[[14]] > harvest_area[[4]] &
>            harvest_area[[14]] > harvest_area[[5]] &
>            harvest_area[[14]] > harvest_area[[6]] &
>            harvest_area[[14]] > harvest_area[[7]] &
>            harvest_area[[14]] > harvest_area[[8]] &
>            harvest_area[[14]] > harvest_area[[9]] &
>            harvest_area[[14]] > harvest_area[[10]] &
>            harvest_area[[14]] > harvest_area[[11]] &
>            harvest_area[[14]] > harvest_area[[12]] &
>            harvest_area[[14]] > harvest_area[[13]] &
>            harvest_area[[14]] > harvest_area[[15]] &
>            harvest_area[[14]] > harvest_area[[16]], 14,
>
>        con(harvest_area[[15]] > harvest_area[[1]] &
>            harvest_area[[15]] > harvest_area[[2]] &
>            harvest_area[[15]] > harvest_area[[3]] &
>            harvest_area[[15]] > harvest_area[[4]] &
>            harvest_area[[15]] > harvest_area[[5]] &
>            harvest_area[[15]] > harvest_area[[6]] &
>            harvest_area[[15]] > harvest_area[[7]] &
>            harvest_area[[15]] > harvest_area[[8]] &
>            harvest_area[[15]] > harvest_area[[9]] &
>            harvest_area[[15]] > harvest_area[[10]] &
>            harvest_area[[15]] > harvest_area[[11]] &
>            harvest_area[[15]] > harvest_area[[12]] &
>            harvest_area[[15]] > harvest_area[[13]] &
>            harvest_area[[15]] > harvest_area[[14]] &
>            harvest_area[[15]] > harvest_area[[16]], 15,
>
>        con(harvest_area[[16]] > harvest_area[[1]] &
>            harvest_area[[16]] > harvest_area[[2]] &
>            harvest_area[[16]] > harvest_area[[3]] &
>            harvest_area[[16]] > harvest_area[[4]] &
>            harvest_area[[16]] > harvest_area[[5]] &
>            harvest_area[[16]] > harvest_area[[6]] &
>            harvest_area[[16]] > harvest_area[[7]] &
>            harvest_area[[16]] > harvest_area[[8]] &
>            harvest_area[[16]] > harvest_area[[9]] &
>            harvest_area[[16]] > harvest_area[[10]] &
>            harvest_area[[16]] > harvest_area[[11]] &
>            harvest_area[[16]] > harvest_area[[12]] &
>            harvest_area[[16]] > harvest_area[[13]] &
>            harvest_area[[16]] > harvest_area[[14]] &
>            harvest_area[[16]] > harvest_area[[15]], 16, 0
>
> ))))))))))))))))
>
>
> -------------------------------------------------------------------------
> Tyler Frazier
> Center for Development Research (ZEF-C)
> University of Bonn
> 53113 Bonn, DE
> +49 (0) 228 73 4949 (office)
> +49 (0) 152 1018 2718 (handy)
> -------------------------------------------------------------------------
>
>
>
>
>
>
>
>         [[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