An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20140525/7f6326de/attachment.pl>
Raster overlay / calc function with NA values
2 messages · Katrina Bennett, Lyndon Estes
Hi Katrina,
I might have misunderstood what you are trying to do, but its seems
the main problem is that you are using the calc function rather than
overlay. Also, I think you don't need the separate function for what
you are trying to achieve, if you are just trying to apply this to two
rasters:
# function not necessary, in the form I have written here, this would
produce the same results as your
# versions because sum() returns NA if you don't specify
# na.rm = TRUE
fun.calc <- function(x) {
tree.w <- sum(x[[1]] / (100 - x[[2]])) # but summing is superfluous
here in any case
return(tree.w)
}
# So, based on the example, I think this achieves what you want to do.
r1 <- raster(nrow=50, ncol = 50)
r1[] <- sample(80:100, size=ncell(r1), replace = TRUE) # added in
some variation here
r1[4:10,] <- NA
r2 <- raster(nrow=50, ncol = 50)
r2[] <- 40
r2[9:15,] <- NA
r3 <- r1 / (100 - r2) # simple raster calculation
plot(r3)
# If you did want to use the function, you could do this.
st <- stack(r1, r2)
r4 <- calc(st, fun.calc)
plot(r3 - r4)
Is this what you were trying to do, or do you want to do a calculation
over more rasters?
Best, Lyndon
On Sun, May 25, 2014 at 4:19 PM, Katrina Bennett <kebennett at alaska.edu> wrote:
I am trying to run a function over two rasters. I am not sure what I'm doing incorrectly, but I can not seem to get the answer I'm looking for, even though the vectorized form of this function works well. I posted this question on Stack Overflow but I think this is a raster issue... hence I thought I might try this forum instead ( http://stackoverflow.com/questions/23852409/raster-calc-function-issue-with-na-data/23852610?noredirect=1#23852610 ). library(raster) fun.calc <- function(x, y, ...) { tree.w <- sum(x / (100 - y), ...) ifelse(is.na(x) | is.na(y), NA, tree.w) } fun.calc1 <- function(x, y, ...) { tree.w <- sum(x / (100 - y), ...) length.xy <- length(which(!is.na(x) & !is.na(y))) ifelse(is.na(x) | is.na(y), NA, tree.w/length.xy) } r1 <- raster(nrow=50, ncol = 50) r1[] <- 90 r1[4:10,] <- NA r2 <- raster(nrow=50, ncol = 50) r2[] <- 40 r2[9:15,] <- NA #This works fun.calc(90, 40) fun.calc(90, NA) #This, I have to add in a division by the length of the non-NA data values, why? fun.calc1(r1[,,1:length(r1)],r2[,,1:length(r2)], na.rm=T) #My data are rasters, and I wish to produce the data in a raster for output as a GeoTIff #This produced no information overlay(r1, r2, fun=fun.calc1, na.rm=T) #Throws an error overlay(r1[,,1], r2[,,1], fun=fun.calc1, na.rm=T) #Error in (function (classes, fdef, mtable) : # unable to find an inherited method for function 'overlay' for signature '"numeric", "numeric"' Thank you, Katrina [[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