[R-sig-Geo] questions on RasterStack/Brick

lberner at whrc.org lberner at whrc.org
Fri Jul 29 22:48:47 CEST 2011


Apologies-- there was an error in the stack correlation function of my last
post. Below is an updated version of the function.  

stack.correlation <- function(stack1, stack2, cor.method, na.val, na.rm =
T){
  # output template
  cor.map <- raster(stack1)
  # combine stacks
  combined.stack <- stack(stack1,stack2)
  NAvalue(combined.stack) = na.val
  
  # The function takes a vector of cell values (z-dimension), partitions it
in half, then correlates
  # the two sections, returning the correlation coefficient.
  stack.sequence.cor <- function(full.vec,na.rm=T){
    vec1<-full.vec[1:(length(full.vec)/2)]
    vec2<-full.vec[(length(full.vec)/2+1):length(full.vec)]
    return(cor(vec1,vec2, method=cor.method))
    }

  # Apply the function above to each xy cell and write the correlation
  # coefficient to the output template.
  cor.map <- stackApply(combined.stack, indices = rep(1,
length(combined.stack)), 
    fun = stack.sequence.cor, na.rm = TRUE)
  return(cor.map)
}

example call: 
my.cor.map <- stack.correlation(august_temp, july_temp, "spearman", -9999)
#plot(my.cor.map)


Logan


--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/questions-on-RasterStack-Brick-tp5553580p6634917.html
Sent from the R-sig-geo mailing list archive at Nabble.com.



More information about the R-sig-Geo mailing list