0

I have two distinct tasks to complete- find the index of the maximum value in each cell of a stack of 12 rasters. There are 12 rasters because each raster is the mean monthly precipitation over 30 years. Then I want to reorder another raster stack of 360 layers, where each layer is the monthly precip over 30 years such that the resulting raster stack has cells that start at the index found to be the of the 12 mean monthly precip values and then continues with the rest of the monthly precip values.

So for example, I have monthly precip values Jan -Dec 1981, 1982 etc. However, I have found that the wettest month is July i.e. 7th month), so I want to reorder such for each pixel the stack starts from July 1981 until Dec 2020.

If I were to do this with a list of vector and not rasters, I would do

twleve_values<-c(11.630832  19.372524  16.788150   4.051670  14.857163 186.173774 420.738594 304.162227 161.079527  23.955036   8.945979   3.019485)
max_tweleve_value<-which.max(twelve_values) #this would result in 7 because the max value 420.738 is the 7th value

if(max_twelve_value > 1) other_360_values[c(max_twelve_value:(length(other_360_values) - (13-max_twelve_value)))] else other_360_values 

I am stuck at the first task of finding the index of the wettest month in the stack of 12 rasters. I have tried

x<-stackApply(12_raster_stack, indices = rep(1,12), fun=which.max, na.rm = TRUE)

Error in FUN(newX[, i], ...) : unused argument (na.rm = TRUE) and

x<-calc(mean_monthly_ppt, fun= whiches.max)

Error in .calcTest(x[1:5], fun, na.rm, forcefun, forceapply) : cannot use this function

How do I move ahead?

0

For the first task use calc:

library(raster)

r <- raster()

set.seed(123)

l <- list()

for(i in 1:12){
  l[[i]] <- setValues(r,sample(c(NA,1:100),size = 64800,replace = T))
}

s <- stack(l)

max_ <- calc(s,which.max)

I used NA values for showing you the function works with that kind of values:

plot(max_)

enter image description here

I'm not really sure about your second task. If you're trying to reorder layers by pixel, I don't think you can accomplish it. You can reoder pixels among layers, but you aim isn't clear for me.

4
  • Your suggestion does not work. I have tried calc(stack, which.max) and the error I get now is Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘writeValues’ for signature ‘"RasterBrick", "list"’ – tg110 Mar 1 at 20:57
  • I tried just which.max(s) and it worked. I do not understand why this is working with which.max() and not with calc() when logically it should work with calc() – tg110 Mar 1 at 21:22
  • The error says the issue, did you try loading rasters with stack or brick? – aldo_tapia Mar 2 at 1:02
  • I loaded all the rasters and made it a stack. As per my comment, I tried which.max(stack) and it worked. I am moving on to the second task now – tg110 Mar 2 at 8:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.