Yan,
It would be much easier to look at this if you send this with some code
generating a small example data set, and with line breaks.
Robert



On Mon, Mar 25, 2013 at 7:45 AM, Boulanger, Yan <
Yan.Boulanger@rncan-nrcan.gc.ca> wrote:

> Hi folks!
>
> I have a large raster representing the biomass of an "unknown" species for
> a given ecozone (ecozones > ecoregions>ecodistricts). I would like to
> create a new raster where the value of each pixel where biomass > 0 would
> be randomly attributed to a number from 1 to 11 (which represent a
> "known-species number" according to a given probability. These
> probabilities change regionally, according to ecoregions (there are 21
> ecoregions, numbered from 2 to 22).
>
> I have already these probabiilties :
>
> For example, for ecoregion no. 22 (see $PROB_BIOMASS):
>
> >PROB_PRES_ecoregions_BS_EAST[[22]]
>
> [[22]]
>            NAMES zone        sum No.SPP COVER PROB_BIOMASS   PROB_CON
> PROB_HARD
> 21      ABIE.BAL   22  7481017.2      2     C  0.078286887 0.27207557
> 0.0000000
> 42  ACER.SPP.ALL   22 23565670.5      8     H  0.246608573 0.00000000
> 0.4706303
> 63      BETU.ALL   22  4540250.3     11     H  0.047512531 0.00000000
> 0.0906734
> 84      BETU.PAP   22 10183401.7     10     H  0.106566633 0.00000000
> 0.2033728
> 105     LARI.LAR   22   576403.6      5     C  0.006031913 0.02096310
> 0.0000000
> 126     PICE.GLA   22  3008975.1      7     C  0.031488137 0.10943279
> 0.0000000
> 147     PICE.MAR   22  3316499.0      1     C  0.034706294 0.12061707
> 0.0000000
> 168     PINU.BAN   22  1503133.6      3     C  0.015729900 0.05466716
> 0.0000000
> 189     PINU.STR   22  8570144.7      4     C  0.089684321 0.31168582
> 0.0000000
> 210 POPU.SPP.ALL   22 11783249.8      9     H  0.123308625 0.00000000
> 0.2353234
> 231 THUJ.SPP.ALL   22  3039927.5      6     C  0.031812045 0.11055849
> 0.0000000
>
>
> UNKNOWN <- raster("C:\\yan\\LANDIS\\SPP\\UNKNOWN_250m_wgtd.tif")
> UNKNOWN_BS_EAST_ras <- crop(UNKNOWN,BS_EAST) UNKNOWN_BS_EAST_ras <-
> overlay(UNKNOWN_BS_EAST_ras, ecoregions_BS_EAST_ras ,
> fun=function(x,y){return(x*y^0)}) UNKNOWN_ecoregions_BS_EAST_s <-
> stack(UNKNOWN_BS_EAST_ras,ecoregions_BS_EAST_ras)
>
> I created a RasterStack combining the unknown biomass layer as well as the
> ecoregion layer. I also created an empty raster to which randomly selected
> values would be fed in. To do the oervall job, I wished to use the overlay
> function but this does not seem to work.
>
> I got this error message (sorry, it's in french):
> Erreur dans FUN(X[[1L]], ...) :
>   aucun slot de nom "names" pour cet objet de la classe ".SingleLayerData"
>
> Moreover, it seems that my function, if working at the end, would have to
> go through to the overall extent of the raster at each "i" iteration to
> create the final raster. Is there a way to get this works more efficiently?
>
> Here is the (failed) code :
>
> e <- extent(UNKNOWN_BS_EAST_ras)
> SPP_replace_unknown <-raster(e)
> projection(SPP_replace_unknown) <- "+proj=lcc +lat_1=49 +lat_2=77 +lat_0=0
> +lon_0=-95 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs
> +towgs84=0,0,0"
>
>
> for (i in
> min(BIOMASS_TOTAL_LIVE_ecoregions_BS_EAST$zone):max(BIOMASS_TOTAL_LIVE_ecoregions_BS_EAST$zone)){
> ## which is from 2 to 22 fun_replace <- function(x,y)
> {return(ifelse(x[[1]]>0 & x[[2]]==i,
> sample(PROB_PRES_ecoregions_BS_EAST[[i]]$No.SPP,prob=PROB_PRES_ecoregions_BS_EAST[[i]]$PROB_BIOMASS),
> y))} SPP_replace_unknown <- overlay(UNKNOWN_ecoregions_BS_EAST_s,
> SPP_replace_unknown, fun=fun_replace)
>
> > sessionInfo()
> R version 2.15.2 (2012-10-26)
> Platform: x86_64-w64-mingw32/x64 (64-bit)
>
> locale:
> [1] LC_COLLATE=French_Canada.1252  LC_CTYPE=French_Canada.1252
>  LC_MONETARY=French_Canada.1252 LC_NUMERIC=C
> LC_TIME=French_Canada.1252
>
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
>
> other attached packages:
> [1] foreign_0.8-51 raster_2.1-16  rgdal_0.8-5    sp_1.0-6
>
> loaded via a namespace (and not attached):
> [1] grid_2.15.2     lattice_0.20-10 tools_2.15.2
> }
>
>
> Many thanks!
>
> Yan
>
>
>
>
>         [[alternative HTML version deleted]]
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo@r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>

	[[alternative HTML version deleted]]

