[R-sig-Geo] Bioclimatic variables - wettest quarter

Robert J. Hijmans r.hijmans at gmail.com
Wed Nov 10 05:53:50 CET 2010


Hi Brian,

This:

>> avg <- overlay(mon_stac, fun=maxmonth)

 does not work because overlay requires multiple (at least 2) Raster
objects. In your case RasterLayer objects

avg <- overlay(a, b, c, etc... , fun=maxmonth)

An alternative would be to use the list of RasterLayers in the
RasterStack and the do.call mechanism:

avg <- do.call(overlay, c(mon_stack at layers, fun=maxmonth))

After fixing the which.max problem as Jacob indicated. I have also
fixed dismo::biovars to deal with that.

Robert


On Mon, Nov 8, 2010 at 5:29 PM, Brian Oney <zenlines at gmail.com> wrote:
> Hello Robert,
>
> Thank you for the suggestions.
> The first one:
> mon_stac <- stack(paste(pc_clim,"1975_prec",1:12,".asc", sep=""))
> plot(mon_stac * 10) # gives me twelve plots of horrendous amounts of rain
>  maxmonth <- function(a,b,d,e,f,g,i,j,k,l,m,n){
> +                     o <- a + b + d
> +                     p <- b + d + e
> +                     r <- d + e + f
> +                     u <- e + f + g
> +                     v <- f + g + i
> +                     w <- g + i + j
> +                     x <- i + j + k
> +                     y <- j + k + l
> +                     z <- k + l + m
> +                     aa <- l + m + n
> +                     ab <- m + n + a
> +                     ac <- n + a + b
> +                     #ad <- which.max(c(o,p,r,u,v,w,x,y,z,aa,ab,ac))
> +                     ad <- calc(stack(o,p,r,u,v,w,x,y,z,aa,ab,ac), fun=
> which.max)
> +                     return(ad)}
>>
>> avg <- overlay(mon_stac, fun=maxmonth)
> Error in rep.int(names(x), lapply(x, length)) :
>  no function to return from, jumping to top level
> Error in .overlayList(rasters, fun = fun, filename = filename, ...) :
>  cannot use this formula
>
> When I use uncomment the which.max attempt above, I still get the same
> "Error in .overlayList(rasters, fun = fun, filename = filename, ...) :
>  cannot use this formula; lenghts do not match" error.
>
>
> In trying it manually, with map algebra:
>
> a <- raster(paste(pc_clim,"1975_prec",1,".asc", sep=""))
> b <- raster(paste(pc_clim,"1975_prec",2,".asc", sep=""))
> d <- raster(paste(pc_clim,"1975_prec",3,".asc", sep=""))
> e <- raster(paste(pc_clim,"1975_prec",4,".asc", sep=""))
> f <- raster(paste(pc_clim,"1975_prec",5,".asc", sep=""))
> g <- raster(paste(pc_clim,"1975_prec",6,".asc", sep=""))
> i <- raster(paste(pc_clim,"1975_prec",7,".asc", sep=""))
> j <- raster(paste(pc_clim,"1975_prec",8,".asc", sep=""))
> k <- raster(paste(pc_clim,"1975_prec",9,".asc", sep=""))
> l <- raster(paste(pc_clim,"1975_prec",10,".asc", sep=""))
> m <- raster(paste(pc_clim,"1975_prec",11,".asc", sep=""))
> n <- raster(paste(pc_clim,"1975_prec",12,".asc", sep=""))
>
> o <- a + b + d
> p <- b + d + e
> r <- d + e + f
> u <- e + f + g
> v <- f + g + i
> w <- g + i + j
> x <- i + j + k
> y <- j + k + l
> z <- k + l + m
> # Try it this this to see if the double character object name are the
> problem.
> A <- l + m + n
> B <- m + n + a
> E <- n + a + b
> # I get the following:
> ad <- calc(stack(o,p,r,u,v,w,x,y,z,A,B,E), fun= which.max)
> #Error in .local(x, values) : values must be numeric, integer or logical.
>
> # Try stacking it:
> G <- stack(o,p,r,u,v,w,x,y,z,A,B,E)
> ad <- calc(G, fun=which.max)
> #Error in .local(x, values) : values must be numeric, integer or logical.
>
> Jacob made some suggestions that also produced a similar error.
>
> Just in case:
>> sessionInfo()
> R version 2.11.0 (2010-04-22)
> i386-pc-mingw32
>
> locale:
> [1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
> States.1252    LC_MONETARY=English_United States.1252 LC_NUMERIC=C
> [5] LC_TIME=English_United States.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
>  [1] rgdal_0.6-27    dismo_0.5-6     rJava_0.8-7     raster_1.6-10
> maps_2.1-3      gpclib_1.5-1    maptools_0.7-34 lattice_0.18-5  sp_0.9-64
>     foreign_0.8-40
>
> loaded via a namespace (and not attached):
> [1] grid_2.11.0  tools_2.11.0
>
> My extent is western North America from Canada to Mexico with a huge amount
> of NA cells over the ocean, if this helps.
> Thank you for the help.
>
> Cheers,
> Brian
>
>
>
>
>



More information about the R-sig-Geo mailing list