[R-sig-Geo] which.max() limit in raster package for RasterBrick
Tyler Frazier
tyler.j.frazier at icloud.com
Tue Sep 23 20:01:14 CEST 2014
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
))))))))))))))))
didnt 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]]
More information about the R-sig-Geo
mailing list